简体   繁体   English

PostgreSQL带WHERE子句的generate_series

[英]PostgreSQL generate_series with WHERE clause

I'm having an issue generating a series of dates and then returning the COUNT of rows matching that each date in the series. 我在生成一系列日期时遇到问题,然后返回与该系列中每个日期匹配的COUNT行。

SELECT generate_series(current_date - interval '30 days', current_date, '1 day':: interval) AS i, COUNT(*)
FROM download
WHERE product_uuid = 'someUUID'
AND created_at = i
GROUP BY created_at::date
ORDER BY created_at::date ASC

I want the output to be the number of rows that match the current date in the series. 我希望输出为与系列中当前日期匹配的行数。

05-05-2018, 35
05-06-2018, 23
05-07-2018, 0
05-08-2018, 10
...

The schema has the following columns: id , product_uuid , created_at . 该架构包含以下列: idproduct_uuidcreated_at Any help would be greatly appreciated. 任何帮助将不胜感激。 I can add more detail if needed. 如果需要,我可以添加更多细节。

Put the table generating function in the from and use a join : 将表生成函数放在from并使用join

SELECT g.dte, COUNT(d.product_uuid)
FROM generate_series(current_date - interval '30 days', current_date, '1 day':: interval
                    ) gs(dte) left join
     download d
     on d.product_uuid = 'someUUID' AND
        d.created_at::date = g.dte
GROUP BY g.dte
ORDER BY g.dte;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM