联接PostgreSQL中每一行的所有行

时间:2018-07-17 18:14:13

标签: sql postgresql postgresql-10

我有一个类似于以下内容的数据集:

CREATE TABLE revenue (
    employee_id int,
    job_id int,
    date date,
    revenue numeric (9,2)
);

INSERT INTO revenue (employee_id, job_id, date, revenue) VALUES (1, 123, '2018-07-16', 100);
INSERT INTO revenue (employee_id, job_id, date, revenue) VALUES (1, 124, '2018-07-17', 100);
INSERT INTO revenue (employee_id, job_id, date, revenue) VALUES (2, 125, '2018-07-14', 100);
INSERT INTO revenue (employee_id, job_id, date, revenue) VALUES (2, 126, '2018-07-16', 100);

我编写了以下查询,并具有以下输出:

SELECT employee_id, generate_series::date, SUM(revenue) as revenue
FROM generate_series('2018-07-14'::date, '2018-07-17'::date, '1 day')
LEFT JOIN revenue ON revenue.date = generate_series::date
GROUP BY employee_id, generate_series::date
ORDER BY employee_id, generate_series::date;

+---------------+-------------------+-----------+
| employee_id   | generate_series   | revenue   |
|---------------+-------------------+-----------|
| 1             | 2018-07-16        | 100.00    |
| 1             | 2018-07-17        | 100.00    |
| 2             | 2018-07-14        | 100.00    |
| 2             | 2018-07-16        | 100.00    |
| <null>        | 2018-07-15        | <null>    |
+---------------+-------------------+-----------+
SELECT 5
Time: 0.010s
super> 

但是我的目标是即使没有收入也要为所有员工提供所有约会。

这是我想要的输出:

+---------------+-------------------+-----------+
| employee_id   | generate_series   | revenue   |
|---------------+-------------------+-----------|
| 1             | 2018-07-14        |   0.00    |
| 1             | 2018-07-15        |   0.00    |
| 1             | 2018-07-16        | 100.00    |
| 1             | 2018-07-17        | 100.00    |
| 2             | 2018-07-14        | 100.00    |
| 2             | 2018-07-15        |   0.00    |
| 2             | 2018-07-16        | 100.00    |
| 2             | 2018-07-17        |   0.00    |
+---------------+-------------------+-----------+

有人知道如何实现这一目标吗?

1 个答案:

答案 0 :(得分:2)

使用cross join生成行,然后使用left join引入数据:

select e.employee_id, gs.dte, coalesce(revenue, 0) as revenue
from generate_series('2018-07-14'::date, '2018-07-17'::date, '1 day') gs(dte) cross join
     (select distinct employee_id from revenue) e left join
     revenue r
     on r.employee_id = e.employee_id and r.date = gs.dte
order by employee_id, gs.date;

我认为group by是不必要的。每个员工和日期最多只能有一行。