I'm trying to build a query using the various examples I found here but with little success so far:
SELECT count(o.hist_create) AS daily_count, d.day AS date_column
FROM (
SELECT day::date FROM generate_series(
(SELECT hist_create FROM orders ORDER BY hist_create LIMIT 1),
CURRENT_TIMESTAMP,
interval '1 day')day)d
LEFT JOIN orders o ON date_trunc('day', o.hist_create) = d.day
WHERE o.customerId = '1234'
GROUP BY d.day
ORDER BY d.day;
So here I want to count orders on a day to day basis from the first order's timestamp to today for a specific customer, and while this works, it doesn't fetch the dates when there was no order for this customer. I would like to see something like:
number | date
15| 06-12-2017
0 | 07-12-2017
9 | 08-12-2017
11| 09-12-2017
But I get:
number | date
15| 06-12-2017
9 | 08-12-2017
11| 09-12-2017
I thought the join would take care of that and it seems this solution worked for others, maybe not when targeting a customer the way I do, though?
The data type of hist_create
is timestamp
.