[Solved] SQL query for formatted output [closed]


I assumed Time column Value are fixed and that are 1,3,5,7.

Below Query is similar to what you wanted to achieve.

select distinct T.id,T.iMid,
  (select amount from myTable
  where time = 1 and id = T.id and iMid = T.iMid) as Time1,
  (select amount from myTable
  where time = 3 and id = T.id and iMid = T.iMid) as Time2,
  (select amount from myTable
  where time = 5 and id = T.id and iMid = T.iMid) as Time3,
  (select amount from myTable
  where time = 7 and id = T.id and iMid = T.iMid) as Time4,
  2 as level
  from myTable T
 union All
select distinct T.id,T.iMid,1,3,5,7,1
  from myTable T
 order by id,level

Here is the SQLFiddel DEMO with Similar pattern as you mentioned in Your Question.

solved SQL query for formatted output [closed]