Reputation: 771
I am trying to create a select with record history within 30 days, where the values column gets a count, I just wish it would add the values returned for each day.
QUERY
SELECT
date_update AS Time,
SUM(COUNT(values)) as "Value"
FROM
tb_get_metrics
WHERE
data_update >= CURRENT_DATE - 30
GROUP BY Time
ORDER BY Time
Example OUTPUT:
Time Value
2019-10-14 09:46:54.789772 30
2019-10-15 09:46:54.789772 50
2019-10-16 09:46:54.789772 70
SELECT * FROM tb_get_metrics
date_update(TimeStamp) value(String)
2019-10-14 09:46:54.789772 apple
2019-10-14 09:46:55.789772 apple
2019-10-14 09:46:56.789772 apple
2019-10-14 09:46:57.789772 apple
2019-10-14 09:46:58.789772 apple
2019-10-14 09:46:59.789772 apple
2019-10-14 09:47:00.789772 apple
2019-10-14 09:46:01.789772 apple
2019-10-14 09:46:02.789772 apple
2019-10-14 09:46:03.789772 apple
2019-10-14 09:46:04.789772 apple
2019-10-14 09:46:05.789772 apple
2019-10-15 09:46:03.789772 potato
2019-10-15 09:46:04.789772 potato
2019-10-15 09:46:05.789772 potato
...
Upvotes: 1
Views: 1893
Reputation: 9958
By truncating the times off the timestamps, you can focus only on the date, which is what your WHERE
clause seeks to do:
postgres=# SELECT
date_update::date AS "Date",
COUNT(values) as "Count"
FROM
tb_get_metrics
WHERE
date_update::date >= CURRENT_DATE - 30
GROUP BY "Date"
ORDER BY "Date";
Date | Count
------------+-------
2019-10-14 | 24
2019-10-15 | 6
(2 rows)
Upvotes: 1
Reputation: 164064
Get the date part only from each timestamp to group by:
SELECT
date_update::date AS Time,
COUNT(values) AS "Value"
FROM tb_get_metrics
WHERE date_update >= CURRENT_DATE - 30
GROUP BY Time
ORDER BY Time
Upvotes: 1
Reputation: 121474
You need to round the times to dates:
SELECT
date_update::date AS "Date",
COUNT(values) as "Value"
FROM
tb_get_metrics
WHERE
data_update >= CURRENT_DATE - 30
GROUP BY Time
ORDER BY Time
Upvotes: 1