按周分组并汇总Postgresql查询中的左连接字段

时间:2019-02-01 14:22:41

标签: sql postgresql

我想每周评估每个客户在我的活动中的参与率。我的桌子:

  • event具有一个start_datemax_nb_of_bookingsgroup_id字段
  • booking的{​​{1}}字段链接到事件ID。

我目前有给定组每周的预订总数(:

event_id

哪个给了我类似的东西

select count(distinct booking.id) as bookings,
       date_trunc('week', event.start_date::date) as week
from event
       left join booking on event.id = booking.event_id
where event.group_id = 2
group by week
order by week desc

到目前为止,太好了。我还可以获取可用预订地点的累计数量:

bookings | week
-------------------------------------
8        | 2019-02-04 00:00:00.000000
39       | 2019-01-28 00:00:00.000000
39       | 2019-01-21 00:00:00.000000
35       | 2019-01-14 00:00:00.000000
39       | 2019-01-07 00:00:00.000000
14       | 2018-12-31 00:00:00.000000
0        | 2018-12-24 00:00:00.000000

哪个给我:

select sum(event.max_nb_of_bookings) as places,
       date_trunc('week', event.start_date::date) as week
from event
where event.group_id = 2
group by week, event.id
order by week desc

现在我想要百分比:

places | week
-----------------------------------
54     | 2019-02-04 00:00:00.000000
54     | 2019-01-28 00:00:00.000000
63     | 2019-01-21 00:00:00.000000
48     | 2019-01-14 00:00:00.000000
49     | 2019-01-07 00:00:00.000000
36     | 2018-12-31 00:00:00.000000
12     | 2018-12-24 00:00:00.000000

我明白了:

select round(1.0 * count(distinct booking.id) / sum(event.max_nb_of_bookings) * 100, 2) as filling,
       date_trunc('week', event.start_date::date) as week
from event
left join booking on event.id = booking.event_id
where event.group_id = 2
group by week
order by week desc

这很奇怪,因为其中大多数应该超过50%。当我要求两个单独的值时,我看到了问题:

filling | week
------------------------------------
12.12   | 2019-02-04 00:00:00.000000
16.25   | 2019-01-28 00:00:00.000000
16.05   | 2019-01-21 00:00:00.000000
16.67   | 2019-01-14 00:00:00.000000
17.41   | 2019-01-07 00:00:00.000000
14.58   | 2018-12-31 00:00:00.000000
0       | 2018-12-24 00:00:00.000000
select count(distinct booking.id) as bookings,
       sum(event.max_nb_of_bookings) as places,
       date_trunc('week', event.start_date::date) as week
from event
       left join booking on event.id = booking.event_id
where event.group_id = 2
group by week
order by week desc

似乎一个选择语句中的“与众不同”正在混淆另一条语句中的“和”。如果我尝试按事件分组,那么我会丢失每周的金额...我可以寻求帮助

1 个答案:

答案 0 :(得分:0)

该问题与您用于总和(event.max_nb_of_bookings)的计数(不同的booking.id)的不同分组有关

例如:

在下面的查询中,您要计算每周的不同booking.id

select count(distinct booking.id) as bookings,
       date_trunc('week', event.start_date::date) as week
  from event
left join booking on event.id = booking.event_id
where event.group_id = 2
group by week /*Note the group by is by week only*/

在这里,您要按事件汇总每周event.max_nb_of_bookings。id

select sum(event.max_nb_of_bookings) as places,
       date_trunc('week', event.start_date::date) as week
from event
where event.group_id = 2
group by week, event.id /*Group by event.id and week*/

因此在您的记录集中,您可能会有如下结果集

places | week                       |event.id  
-----------------------------------
54     | 2019-02-04 00:00:00.000000 |1
12     | 2019-02-04 00:00:00.000000 |2 ---> There can be a record as follows 
54     | 2019-01-28 00:00:00.000000 |3
63     | 2019-01-21 00:00:00.000000 |3
48     | 2019-01-14 00:00:00.000000 |4
49     | 2019-01-07 00:00:00.000000 |5
36     | 2018-12-31 00:00:00.000000 |6
12     | 2018-12-24 00:00:00.000000 |7

因此

的输出

选择sum(event.max_nb_of_bookings)作为地方,        date_trunc('week',event.start_date :: date)作为星期 从事件 其中event.group_id = 2 按周分组

将是

places | week                       
-----------------------------------
66     | 2019-02-04 00:00:00.000000 
54     | 2019-01-28 00:00:00.000000 
63     | 2019-01-21 00:00:00.000000 
48     | 2019-01-14 00:00:00.000000 
49     | 2019-01-07 00:00:00.000000 
36     | 2018-12-31 00:00:00.000000 
12     | 2018-12-24 00:00:00.000000