我需要创建一个返回
的PostgreSQL查询重要的是每一天都会出现在结果中,即使当天没有找到任何对象也是如此。 (之前已经讨论过这个问题,但是在我的具体案例中我无法解决问题。)
首先,我找到了sql query to generate a range of days,我可以加入其中:
SELECT to_char(date_trunc('day', (current_date - offs)), 'YYYY-MM-DD')
AS date
FROM generate_series(0, 365, 1)
AS offs
结果:
date
------------
2013-03-28
2013-03-27
2013-03-26
2013-03-25
...
2012-03-28
(366 rows)
现在我正在尝试将其加入名为“sharer_emailshare”的表中,该表具有“已创建”列:
Table 'public.sharer_emailshare'
column | type
-------------------
id | integer
created | timestamp with time zone
message | text
to | character varying(75)
这是我到目前为止最好的GROUP BY
查询:
SELECT d.date, count(se.id) FROM (
select to_char(date_trunc('day', (current_date - offs)), 'YYYY-MM-DD')
AS date
FROM generate_series(0, 365, 1)
AS offs
) d
JOIN sharer_emailshare se
ON (d.date=to_char(date_trunc('day', se.created), 'YYYY-MM-DD'))
GROUP BY d.date;
结果:
date | count
------------+-------
2013-03-27 | 11
2013-03-24 | 2
2013-02-14 | 2
(3 rows)
期望的结果:
date | count
------------+-------
2013-03-28 | 0
2013-03-27 | 11
2013-03-26 | 0
2013-03-25 | 0
2013-03-24 | 2
2013-03-23 | 0
...
2012-03-28 | 0
(366 rows)
如果我理解正确,这是因为我使用普通(隐含的INNER
)JOIN
,这是预期的行为,discussed in the postgres docs。
我查看了几十个StackOverflow解决方案,所有具有工作查询的解决方案似乎都特定于MySQL / Oracle / MSSQL,我很难将它们转换为PostgreSQL。
那个问this question的人用Postgres找到了他的答案,但把它放在了一段时间前过期的pastebin链接上。
我尝试切换到LEFT OUTER JOIN
,RIGHT JOIN
,RIGHT OUTER JOIN
,CROSS JOIN
,如果为null,则使用CASE
语句转到另一个值中, COALESCE
提供默认值等,但我无法以满足我需要的方式使用它们。
感谢任何帮助!而且我保证我会尽快阅读那本巨大的PostgreSQL书籍;)
答案 0 :(得分:45)
您只需要left outer join
而不是内部联接:
SELECT d.date, count(se.id)
FROM (SELECT to_char(date_trunc('day', (current_date - offs)), 'YYYY-MM-DD') AS date
FROM generate_series(0, 365, 1) AS offs
) d LEFT OUTER JOIN
sharer_emailshare se
ON d.date = to_char(date_trunc('day', se.created), 'YYYY-MM-DD'))
GROUP BY d.date;
答案 1 :(得分:25)
扩展Gordon Linoff的有用答案,我建议进行一些改进,例如:
::date
代替date_trunc('day', ...)
这是我的疑问:
WITH dates_table AS (
SELECT created::date AS date_column FROM sharer_emailshare WHERE showroom_id=5
)
SELECT series_table.date, COUNT(dates_table.date_column), SUM(COUNT(dates_table.date_column)) OVER (ORDER BY series_table.date) FROM (
SELECT (last_date - b.offs) AS date
FROM (
SELECT GENERATE_SERIES(0, last_date - first_date, 1) AS offs, last_date from (
SELECT MAX(date_column) AS last_date, (MAX(date_column) - '1 year'::interval)::date AS first_date FROM dates_table
) AS a
) AS b
) AS series_table
LEFT OUTER JOIN dates_table
ON (series_table.date = dates_table.date_column)
GROUP BY series_table.date
ORDER BY series_table.date
我测试了查询,它产生了相同的结果,加上累计总数的列。
答案 2 :(得分:7)
根据Gordon Linoff的回答,我意识到另一个问题是我有一个WHERE
条款,我在原始问题中没有提及。
而不是裸WHERE
,我做了一个子查询:
SELECT d.date, count(se.id) FROM (
select to_char(date_trunc('day', (current_date - offs)), 'YYYY-MM-DD')
AS date
FROM generate_series(0, 365, 1)
AS offs
) d
LEFT OUTER JOIN (
SELECT * FROM sharer_emailshare
WHERE showroom_id=5
) se
ON (d.date=to_char(date_trunc('day', se.created), 'YYYY-MM-DD'))
GROUP BY d.date;
答案 3 :(得分:2)
我将尝试提供包含一些解释的答案。我将从最小的构建块开始进行工作。
如果运行这样的查询:
SELECT series.number FROM generate_series(0, 9) AS series(number)
您将获得如下输出:
number
--------
0
1
2
3
4
5
6
7
8
9
(10 rows)
这可以变成这样的日期:
SELECT CURRENT_DATE + sequential_dates.date AS date
FROM generate_series(0, 9) AS sequential_dates(date)
哪个会给出这样的输出:
date
------------
2019-09-29
2019-09-30
2019-10-01
2019-10-02
2019-10-03
2019-10-04
2019-10-05
2019-10-06
2019-10-07
2019-10-08
(10 rows)
然后,您可以进行这样的查询(例如),将原始查询作为子查询加入您最终感兴趣的表:
SELECT sequential_dates.date,
COUNT(calendar_items.*) AS calendar_item_count
FROM (SELECT CURRENT_DATE + sequential_dates.date AS date
FROM generate_series(0, 9) AS sequential_dates(date)) sequential_dates
LEFT JOIN calendar_items ON calendar_items.starts_at::date = sequential_dates.date
GROUP BY sequential_dates.date
哪个会给出这样的输出:
date | calendar_item_count
------------+---------------------
2019-09-29 | 1
2019-09-30 | 8
2019-10-01 | 15
2019-10-02 | 11
2019-10-03 | 1
2019-10-04 | 12
2019-10-05 | 0
2019-10-06 | 0
2019-10-07 | 27
2019-10-08 | 24
答案 4 :(得分:0)
我喜欢Jason Swett SQL,但是遇到了一个问题,其中某些日期的计数应该为零而不是一。 运行语句从public.post_call_info选择count(*),其中timestamp :: date ='2020-11-23'count = 0,但小于等于1。
此外,+号给了我一个向前的时间表,因此更改为减号,可以提供当前日期之前9天的数据。
SELECT sequential_dates.date,
COUNT(*) AS call_count
FROM (SELECT CURRENT_DATE - sequential_dates.date AS date
FROM generate_series(0, 9) AS sequential_dates(date)) sequential_dates
LEFT JOIN public.post_call_info ON public.post_call_info.timestamp::date =
sequential_dates.date
GROUP BY sequential_dates.date
order by date desc