Skip to content
Advertisement

Need to create a Totals report from oracle table data

I have a table in oracle similar to the the example table image that I need to have output the segregated totals as shown in the second example results image. I need the sums of each item_type where A & B are just examples for a large number of possible items that can be added.

example table:

enter image description here

example results:

enter image description here

Any help would be appreciated. thanks

Advertisement

Answer

Use conditional aggregation:

SELECT
    store,
    SUM(CASE WHEN item_type = 'A' AND item_status = 'IN'  THEN 1 ELSE 0 END) a_items_in,
    SUM(CASE WHEN item_type = 'A' AND item_status = 'OUT' THEN 1 ELSE 0 END) a_items_out,
    SUM(CASE WHEN item_type = 'B' AND item_status = 'IN'  THEN 1 ELSE 0 END) b_items_in,
    SUM(CASE WHEN item_type = 'B' AND item_status = 'OUT' THEN 1 ELSE 0 END) b_items_out
FROM mytable
GROUP BY store
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement