I have a table, 'commercial_events' that contains both sales ('event_type = 'SALE'') and purchases ('event_type = 'PURCHASE'). So far I'm using two separate queries, to create tabulated output broken-down by the hour. I would like to be able to run a single query, and combine all the data in a single output.
Right now I'm running this query:
SELECT date_trunc('hour',event_tstamp) AS 'Hour', count(*) AS 'Purchases', sum(amount) AS 'Purchases Total'
FROM commercial_events
WHERE event_type = 'PURCHASE' AND state = 'COMPLETED'
GROUP BY 1
ORDER BY 1;
The output it returns looks like this:
Hour | Purchases | Purchases Total
------------------------+--------------+--------------------
2019-12-12 00:00:00+01 | 476 | -533.582000000000
...
When doing the process manually, I also run the below query and combine the 2 outputs manually:
SELECT date_trunc('hour',event_tstamp) AS 'Hour', count(*) AS 'Sales', sum(amount) AS 'Sales Total'
FROM commercial_events
WHERE event_type = 'SALE' AND state = 'COMPLETED'
GROUP BY 1
ORDER BY 1;
But I want the 2 outputs to be combined, like this:
Hour | Sales | Sales Total | Purchases | Purchases Total
------------------------+-----------+-----------------+---------------+--------------------
2019-12-12 00:00:00+01 | 1173 | 2330 | 476 | -533.582000000000
...