sql server - SQL query for formatted output -
i want create sql query, , format output of query.
my table looks this:
time amount id imid --------------------------------- 1 2500 1 4 3 5000 1 4 5 10000 1 4 7 20000 1 4 1 2500 2 8 3 5000 2 8 5 10000 2 8 7 20000 2 8
i want output in following format:-
id imid time1 time2 time3 time4 ---------------------------------- 1 4 1 3 5 7 2500 5000 10000 20000 2 8 1 3 5 7 2500 5000 10000 20000
i've tried:
select sprofileid,imerchantid,'day1','day2','day3','day4',damount (select rm.sprofileid,rm.imerchantid,rc.idays,rc.damount tblrulebymerchant rm join tblalertrulecummulativedata rc on (rm.sprofileid=rc.sprofileid) ) d pivot (max(rm.sprofileid) rm.imerchantid in ('day1','day2','day3','day4')) piv
i assumed time
column value fixed , 1,3,5,7
.
below query similar wanted achieve.
select distinct t.id,t.imid, (select amount mytable time = 1 , id = t.id , imid = t.imid) time1, (select amount mytable time = 3 , id = t.id , imid = t.imid) time2, (select amount mytable time = 5 , id = t.id , imid = t.imid) time3, (select amount mytable time = 7 , id = t.id , imid = t.imid) time4, 2 level mytable t union select distinct t.id,t.imid,1,3,5,7,1 mytable t order id,level
here sqlfiddel demo similar pattern mentioned in question.
Comments
Post a Comment