简体   繁体   中英

making rows to columns in mysql

i have this hour wise data , i want to generate reports where i have to show the revenue every hour in a single row groupy by product,mode,region (sorry for bad format)

      price  product   mode             region         HOUR 

1.     0     p1        direct            reg1          1 
2.     10    p3        indirect          reg2          2 
3.     0     p2        direct            reg1          1 
4.     0     p1        indirect          reg2          2 
5.     0     p2        direct            reg5          3 
6.     0     p1        direct            reg1          3 
7.     0     p1        direct            reg2          3 
8.     0     p3        indirect          reg4          17 
9.     0     p4        direct            reg2          17 
10.    21    p1        direct            reg2          17 


        HOUR1 HOUR2 HOUR3 HOUR4 HOUR5....HOUR23 product   mode    region
  1.    0      10     1     30                  p1     direct       reg1
  2.    0      5      1     10                  p1     indirect     reg2

Unfortunately mysql does not have any inbuilt pivot function so you need to either write a dynamic sql for the unknown set of pivot element or can use the most commonly used pivot generating technique for known set of pivot element, and in your case its known set since you are looking at pivot data from hour1 till hour23 and it could be done as

select
sum( case when HOUR = 1 then price else 0 end) as `HOUR1`,
sum( case when HOUR = 2 then price else 0 end) as `HOUR2`,
sum( case when HOUR = 3 then price else 0 end) as `HOUR3`,
sum( case when HOUR = 4 then price else 0 end) as `HOUR4`,
sum( case when HOUR = 5 then price else 0 end) as `HOUR5`,
sum( case when HOUR = 6 then price else 0 end) as `HOUR6`,
sum( case when HOUR = 7 then price else 0 end) as `HOUR7`,
sum( case when HOUR = 8 then price else 0 end) as `HOUR8`,
sum( case when HOUR = 9 then price else 0 end) as `HOUR9`,
sum( case when HOUR = 10 then price else 0 end) as `HOUR10`,
sum( case when HOUR = 11 then price else 0 end) as `HOUR11`,
sum( case when HOUR = 12 then price else 0 end) as `HOUR12`,
sum( case when HOUR = 13 then price else 0 end) as `HOUR13`,
sum( case when HOUR = 14 then price else 0 end) as `HOUR14`,
sum( case when HOUR = 15 then price else 0 end) as `HOUR15`,
sum( case when HOUR = 16 then price else 0 end) as `HOUR16`,
sum( case when HOUR = 17 then price else 0 end) as `HOUR17`,
sum( case when HOUR = 18 then price else 0 end) as `HOUR18`,
sum( case when HOUR = 19 then price else 0 end) as `HOUR19`,
sum( case when HOUR = 20 then price else 0 end) as `HOUR20`,
sum( case when HOUR = 21 then price else 0 end) as `HOUR21`,
sum( case when HOUR = 22 then price else 0 end) as `HOUR22`,
sum( case when HOUR = 23 then price else 0 end) as `HOUR23`,
product,
mode,
region
from mytable 
group by product,mode,region;

http://sqlfiddle.com/#!9/b2e868/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM