I have a table that looks like this
TIMECODE UNIT_CODE Department Account AMOUNT 20194 10 1000 1000 100 20194 10 2354 1100 150 20194 10 1000 1000 200 20194 10 2354 1000 100 20194 20 500 1000 250 20194 20 500 1100 200
How I need the results to be is like this
TIMECODE UNIT_CODE Department 1000 1100 20194 10 1000 300 NULL 20194 10 2354 100 150 20194 20 500 250 200
hopefully that gives you a better image, but basically I would need to do a SUM depending on the distinct value of the other columns. The accounts that were previously in rows would be changed into columns.
any ideas or help with this would be greatly appreciated
Advertisement
Answer
Try the following, here is the demo.
select TIMECODE, UNIT_CODE, Department, sum(case when Account = 1000 then AMOUNT end) as "1000", sum(case when Account = 1100 then AMOUNT end) as "1100" from myTable group by TIMECODE, UNIT_CODE, Department
Output:
--------------------------------------------------- | TIMECODE UNIT_CODE DEPARTMENT 1000 1100 | --------------------------------------------------- | 20194 20 500 250 200 | | 20194 10 1000 300 null| | 20194 10 2354 100 150 | ---------------------------------------------------