sql - How to turn repeated ranking(1-5) row data to column data in TSQL -


i have table data:

id      sale    weekday 1       12      1 2       15      2 3       16      3 4       17      4 5       18      5 6       11      1 7       13      2 8       14      3 9       15      4 10      20      5 11      25      1 12      14      2 13      18      3 14      21      4 15      11      5 ..      .. 

i'd turn into:

mo   tu     th   fr 12   15   16   17   18 11   13   14   15   20 25   14   18   21   11 .. 

thank you!

try this

select sum(case when weekday = 1 sale else 0 end) mn,        sum(case when weekday = 2 sale else 0 end) tu,        sum(case when weekday = 3 sale else 0 end) we,        sum(case when weekday = 4 sale else 0 end) th,        sum(case when weekday = 5 sale else 0 end) fr (     select *,           row_number()over(partition weekday order id ) seq_no       tablename ) group seq_no 

as mentioned in sample data if table has 5 days week

select sum(case when weekday = 1 sale else 0 end) mn,        sum(case when weekday = 2 sale else 0 end) tu,        sum(case when weekday = 3 sale else 0 end) we,        sum(case when weekday = 4 sale else 0 end) th,        sum(case when weekday = 5 sale else 0 end) fr (     select *,           ( ( row_number()over(order id ) - 1 ) / 5 ) + 1 seq_no       tablename ) group seq_no 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -