请提供以下帮助,请
如果我向您展示我想从查询中得到的结果,也许这会帮助您理解问题......
region shipdate shipday count
Berkshire 2017-02-16 Thursday 3
No Dels 2017-02-16 Friday 0 <<<< This row does not exist in the table
Collection 2017-02-18 Saturday 5
Surrey 2017-02-18 Saturday 1
No Dels 2017-02-19 Sunday 0 <<<< This row does not exist in the table
Wales 2017-02-20 Monday 5
Scotland 2017-02-21 Tuesday 2
No Dels 2017-02-16 Wednesday 0 <<<< This row does not exist in the table
Bucks 2017-02-23 Thursday 2
解释结果。周四,2017-02-16,有3个交付计划到伯克希尔。第二天根本没有送货。星期六有5个收藏和1个送到萨里。
问题是我的查询...
Select o.region, o.shipDate, to_char(o.shipDate, 'Day') as shipDay, count(o.region)
From orders o
Where o.shipDate Between '2017-02-16' And '2017-02-23'
And o.region IS NOT NULL
Group By o.shipDate, o.region
Order By o.shipDate, o.region
产生以下结果......
region shipdate shipday count
Berkshire 2017-02-16 Thursday 3
Collection 2017-02-18 Saturday 5
Surrey 2017-02-18 Saturday 1
Wales 2017-02-20 Monday 5
Scotland 2017-02-21 Tuesday 2
Bucks 2017-02-23 Thursday 2
我的意思是,在聚合计数(o.region)= 0的情况下,显然没有计划交付或收集,因此没有要返回的行。环顾四周,maniek对一个关于列出范围日期的问题有一个很好的答案。
答案是...... Link
select i::date from generate_series('2017-02-16',
'2017-02-23', '1 day'::interval) i
这确实列出了我想要的一系列日期,但我无法弄清楚如何将此查询与我的一起加入。我尝试过的一个例子是......
Select o.region, o.shipDate, to_char(o.shipDate, 'Day') as shipDay, count(o.region)
From orders o
Left Outer Join
(select i::date from generate_series('2017-02-16',
'2017-02-23', '1 day'::interval) i) bookDate On bookDate.i = o.shipDate
Where o.shipDate Between '2017-02-16' And '2017-02-23'
And o.region IS NOT NULL
Group By o.shipDate, o.region
Order By o.shipDate, o.region
无论我使用哪种联接......左,右,左外,右外或甚至内,我的结果总是一样的。
我的问题是如何为表中没有的日期生成一行?