Guest User

CrossTab

a guest
May 13th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.53 KB | None | 0 0
  1. --Script Start--
  2.  
  3. Drop table TmpData_2021;
  4.  
  5. Create table TmpData_2021 (
  6. ID int identity(1,1) primary key,
  7. RM varchar(20) not null,
  8. Month varchar(5) not null,
  9. Price Decimal(20,4) not null,
  10. KG decimal(20,4) not null)
  11.  
  12. --Insert Sample data
  13. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM001','Jan','1000','1')
  14. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM002','Jan','1000','1.2')
  15. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM003','Jan','1000','1.1')
  16. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM004','Jan','1000','1.4')
  17. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM005','Jan','1000','1.2')
  18. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM006','Jan','1000','1')
  19. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM007','Jan','1000','1')
  20. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM008','Jan','1000','1')
  21. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM009','Jan','1000','1.2')
  22. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM010','Jan','1000','1.3')
  23. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM011','Feb','1000','1.1')
  24. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM012','Feb','1500','1.5')
  25. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM001','Feb','1000','1')
  26. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM002','Feb','1200','1.2')
  27. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM003','Feb','1100','1.1')
  28. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM004','Feb','1400','1.4')
  29. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM005','Feb','1200','1.2')
  30. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM006','Feb','1000','1')
  31. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM007','Feb','1000','1')
  32. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM008','Feb','1000','1')
  33. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM009','Feb','1200','1.2')
  34. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM010','Feb','1300','1.3')
  35. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM005','Mar','1200','1.2')
  36. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM006','Mar','1000','1')
  37. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM007','Mar','1000','1')
  38. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM008','Mar','1000','1')
  39. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM009','Mar','1200','1.2')
  40. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM010','Mar','1300','1.3')
  41. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM011','Mar','1100','1.1')
  42. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM012','Mar','2250','1.5')
  43. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM005','Mar','1300','1.2')
  44. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM006','Mar','1300','1')
  45. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM007','Mar','1500','1')
  46. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM008','Mar','1200','1')
  47. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM009','Mar','1100','1.2')
  48. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM010','Mar','1000','1.3')
  49. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM011','Mar','1500','1.1')
  50. Insert into TmpData_2021 (RM, Month, Price, KG )values ( 'RM012','Mar','2550','1.5')
  51.  
  52. --Test data
  53. SELECT TOP (1000) [ID],[RM],[Month],[Price],[KG]
  54.   FROM [MARKETING].[Commercial_Control].[TmpData_2021]
  55.   --where rm = 'RM007'
  56. --Order more in Mar
  57. SELECT  [rm], month, sum(price) TotOrder, sum(kg) TotKG
  58.   FROM [MARKETING].[Commercial_Control].[TmpData_2021]
  59.   --where rm = 'RM007'
  60.   GROUP BY [rm],month
  61. --How to calculate price/kg in sql command and pivot by month
  62. /*
  63. RM         Jan        Feb          Mar
  64. RM001   1000.0     1000.0  
  65. RM002    833.3     1000.0  
  66. RM003    909.1     1000.0  
  67. RM004    714.3     1000.0  
  68. RM005    833.3     1000.0      1041.7
  69. RM006   1000.0     1000.0      1150.0
  70. RM007   1000.0     1000.0      1250.0
  71. RM008   1000.0     1000.0      1100.0
  72. RM009    833.3     1000.0       958.3
  73. RM010    769.2     1000.0       884.6
  74. RM011               909.1      1181.8
  75. RM012              1000.0      1600.0
  76. */
  77. SELECT NT.RM, [Jan], [Feb], [Mar]
  78. from (
  79.     SELECT  [rm], month, sum(price) TotOrder, sum(kg) TotKG
  80.     FROM [TmpData_2021]
  81.     GROUP BY [RM], month
  82. ) as P
  83. -- need total order summation  / total weight summation But Error this line
  84. PIVOT (
  85. SUM(TotOrder) FOR month IN ([Jan], [Feb], [Mar])
  86.  
  87. ) AS NT
  88. GROUP BY NT.RM, [Jan], [Feb], [Mar]
  89. ORDER BY NT.RM
Advertisement
Add Comment
Please, Sign In to add comment