避免使用 sum 的笛卡尔积
Avoid cartesian product using sum
我想总结 tickets
table 中的 stake
,按 customer_id
和 bonus
中的 date_trunc('day')
分组 table.
问题是行数在增加,不知道怎么解决
https://www.db-fiddle.com/f/yWCvFamMAY9uGtoZupiAQ/4
CREATE TABLE tickets (
ticket_id integer,
customer_id integer,
stake integer,
reg_date date
);
CREATE TABLE bonus (
bonus_id integer,
customer_id integer,
reg_date date
);
insert into tickets
values
(1,100, 12,'2019-01-10 11:00'),
(2,100, 10,'2019-01-10 12:00'),
(3,100, 30,'2019-01-10 13:00'),
(4,100, 10,'2019-01-11 14:00'),
(5,100, 15,'2019-01-11 15:00'),
(6,102, 25,'2019-01-10 10:00'),
(7,102, 25,'2019-01-10 11:10'),
(8,102, 13,'2019-01-11 12:40'),
(9,102, 9,'2019-01-12 15:00'),
(10,102, 7,'2019-01-13 18:00'),
(13,103, 15,'2019-01-12 19:00'),
(14,103, 11,'2019-01-12 22:00'),
(15,103, 11,'2019-01-14 02:00'),
(16,103, 11,'2019-01-14 10:00')
;
insert into bonus
values
(200,100,'2019-01-10 05:00'),
(201,100,'2019-01-10 06:00'),
(202,100,'2019-01-10 15:00'),
(203,100,'2019-01-10 15:50'),
(204,100,'2019-01-10 16:10'),
(205,100,'2019-01-10 16:15'),
(206,100,'2019-01-10 16:22'),
(207,100,'2019-01-11 10:10'),
(208,100,'2019-01-11 16:10'),
(209,102,'2019-01-10 10:00'),
(210,102,'2019-01-10 11:00'),
(211,102,'2019-01-10 12:00'),
(212,102,'2019-01-10 13:00'),
(213,103,'2019-01-11 11:00'),
(214,103,'2019-01-11 18:00'),
(215,103,'2019-01-12 15:00'),
(216,103,'2019-01-12 16:00'),
(217,103,'2019-01-14 02:00')
select
customer_id,
date_trunc('day', b.reg_date),
sum(t.stake)
from tickets t
join bonus b using (customer_id)
where date_trunc('day', b.reg_date) = date_trunc('day', t.reg_date)
group by 1,2
order by 1
客户 102 的输出应为:
102,2019-01-10, 50
好的,我想你想获取 tickets
table 列 stake
的汇总数据,并且记录的 customer_id, reg_date
对出现在第二个 tablebonus
,所有的业务都与bonus_id
无关,我说的对吗? bonus
中的customer_id, reg_date
对是重复的,所以你需要一个distinct
,然后join
来自tickets.The的sum
数据完成SQL 结果如下:
with stake_sum as (
select
customer_id,
reg_date,
sum(stake)
from
tickets
group by
customer_id,
reg_date
)
,bonus_date_distinct as (
select
distinct customer_id,
reg_date
from
bonus
)
select
a.*
from
stake_sum a
join
bonus_date_distinct b on a.customer_id = b.customer_id and a.reg_date = b.reg_date order by customer_id, reg_date;
customer_id | reg_date | sum
-------------+------------+-----
100 | 2019-01-10 | 52
100 | 2019-01-11 | 25
102 | 2019-01-10 | 50
103 | 2019-01-12 | 26
103 | 2019-01-14 | 22
(5 rows)
我想总结 tickets
table 中的 stake
,按 customer_id
和 bonus
中的 date_trunc('day')
分组 table.
问题是行数在增加,不知道怎么解决
https://www.db-fiddle.com/f/yWCvFamMAY9uGtoZupiAQ/4
CREATE TABLE tickets (
ticket_id integer,
customer_id integer,
stake integer,
reg_date date
);
CREATE TABLE bonus (
bonus_id integer,
customer_id integer,
reg_date date
);
insert into tickets
values
(1,100, 12,'2019-01-10 11:00'),
(2,100, 10,'2019-01-10 12:00'),
(3,100, 30,'2019-01-10 13:00'),
(4,100, 10,'2019-01-11 14:00'),
(5,100, 15,'2019-01-11 15:00'),
(6,102, 25,'2019-01-10 10:00'),
(7,102, 25,'2019-01-10 11:10'),
(8,102, 13,'2019-01-11 12:40'),
(9,102, 9,'2019-01-12 15:00'),
(10,102, 7,'2019-01-13 18:00'),
(13,103, 15,'2019-01-12 19:00'),
(14,103, 11,'2019-01-12 22:00'),
(15,103, 11,'2019-01-14 02:00'),
(16,103, 11,'2019-01-14 10:00')
;
insert into bonus
values
(200,100,'2019-01-10 05:00'),
(201,100,'2019-01-10 06:00'),
(202,100,'2019-01-10 15:00'),
(203,100,'2019-01-10 15:50'),
(204,100,'2019-01-10 16:10'),
(205,100,'2019-01-10 16:15'),
(206,100,'2019-01-10 16:22'),
(207,100,'2019-01-11 10:10'),
(208,100,'2019-01-11 16:10'),
(209,102,'2019-01-10 10:00'),
(210,102,'2019-01-10 11:00'),
(211,102,'2019-01-10 12:00'),
(212,102,'2019-01-10 13:00'),
(213,103,'2019-01-11 11:00'),
(214,103,'2019-01-11 18:00'),
(215,103,'2019-01-12 15:00'),
(216,103,'2019-01-12 16:00'),
(217,103,'2019-01-14 02:00')
select
customer_id,
date_trunc('day', b.reg_date),
sum(t.stake)
from tickets t
join bonus b using (customer_id)
where date_trunc('day', b.reg_date) = date_trunc('day', t.reg_date)
group by 1,2
order by 1
客户 102 的输出应为:
102,2019-01-10, 50
好的,我想你想获取 tickets
table 列 stake
的汇总数据,并且记录的 customer_id, reg_date
对出现在第二个 tablebonus
,所有的业务都与bonus_id
无关,我说的对吗? bonus
中的customer_id, reg_date
对是重复的,所以你需要一个distinct
,然后join
来自tickets.The的sum
数据完成SQL 结果如下:
with stake_sum as (
select
customer_id,
reg_date,
sum(stake)
from
tickets
group by
customer_id,
reg_date
)
,bonus_date_distinct as (
select
distinct customer_id,
reg_date
from
bonus
)
select
a.*
from
stake_sum a
join
bonus_date_distinct b on a.customer_id = b.customer_id and a.reg_date = b.reg_date order by customer_id, reg_date;
customer_id | reg_date | sum
-------------+------------+-----
100 | 2019-01-10 | 52
100 | 2019-01-11 | 25
102 | 2019-01-10 | 50
103 | 2019-01-12 | 26
103 | 2019-01-14 | 22
(5 rows)