简体   繁体   English

左外连接2列

[英]Left Outer Join with 2 columns

I'm having an issue with a postrgresql join. 我在postrgresql联接方面遇到问题。 I might be approaching it incorrectly, but here's the scenario. 我可能会错误地处理它,但这是场景。

I have a table which contains two relevant columns: dates and months (along with other data). 我有一个包含两个相关列的表:日期和月份(以及其他数据)。 Each date should have the next 5 months associated with it, inclusive. 每个日期应与接下来的5个月(包括首尾两天)相关。 This isn't always the case; 并非总是如此。 I want to find when this isn't the case. 我想找到不是这种情况。 Additionally, there is no guarantee that each date is in the table (for which there should be 5 months), but I have another table which contains these dates. 此外,不能保证每个日期都在表格中(应该有5个月),但是我有另一个包含这些日期的表格。

The table should contain (for one date): 该表应包含(一个日期):

预期

However, due to many possibilities the table may only contain: 但是,由于多种可能性,该表可能仅包含:

可能

I have attempted to find the missing dates by generating a series for the expected dates and joining a series of months that should be associated with the date. 我试图通过为预期日期生成一系列数据并加入一系列与该日期相关联的月份来找到缺失的日期。 I'm running into an issue because I need to join the tables on the two columns I need, so if one doesn't exist, it doesn't make it through the ON or WHERE clause. 我遇到了一个问题,因为我需要将表连接到我需要的两列上,所以如果一个表不存在,就无法通过ON或WHERE子句来实现。

I might need to approach this differently, but here is my current attempt. 我可能需要采取不同的方法,但这是我目前的尝试。

SELECT 
    D.date, JOINMONTH::date, DT.month
FROM
    day D
CROSS JOIN
    generate_series(date_trunc('month', D.date),
                    date_trunc('month', D.date) + INTERVAL '4 months',
                    '1 month') AS JOINMONTH
LEFT JOIN 
    dates_table DT ON D.date = DT.date 
                   AND JOINMONTH::date = DT.month
WHERE
    D.date >= '2018-01-01';

What I would like to see: 我想看到的是:

结果(目标)

EDIT: This db-fiddle gives my full query. 编辑: 这个数据库提琴给了我完整的查询。 I omitted some of the where clause because I thought it was irrelevant, but it seems to be part of the problem. 我省略了一些where子句,因为我认为这无关紧要,但这似乎是问题的一部分。 With this in mind, my selected answer will solve my problem represented by this structure/query but @Gordon Linoff's answer is correct for the original question. 考虑到这一点,我选择的答案将解决此结构/查询代表的问题,但是@Gordon Linoff的答案对原始问题是正确的。

Is this what you are looking for? 这是你想要的?

SELECT D.date, JOINMONTH::date, DT.month
FROM day D CROSS JOIN LATERAL
     generate_series(date_trunc('month', D.date),
                     date_trunc('month', D.date) + INTERVAL '4 months',
                     '1 month') AS JOINMONTH LEFT JOIN
     dates_table DT
     ON GD.date = DT.date AND JOINMONTH::date = DT.month
WHERE D.date >= '2018-01-01' AND DT.date IS NULL;
SELECT D.date, JOINMONTH::date, DT.month
FROM day D 
CROSS JOIN LATERAL
     generate_series(date_trunc('month', D.date),
                     date_trunc('month', D.date) + INTERVAL '4 months',
                     '1 month') AS JOINMONTH 
LEFT JOIN dates_table DT
    ON D.date = DT.date 
    AND JOINMONTH::date = DT.month
    AND DT.source = 'S1' AND
    DT.tf = TRUE
WHERE 
    D.date = '2018-11-02';

I needed to move parts of my where clause into the join itself. 我需要将我的where子句的一部分移到联接本身中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM