【问题标题】:Write a SQL query to return customers booked across multiple years but not booked in 2021编写 SQL 查询以返回已预订多年但未在 2021 年预订的客户
【发布时间】:2021-11-06 08:57:10
【问题描述】:

我需要编写一个查询来回答以下问题:

“提供 2018-2020 年期间但未在 2021 年预订的客户的报告”

我生成了以下查询以按年份分离出如下所示的数据,每个表代表每年的客户预订量。

按年份获取客户预订:

with active_2018 as
(
    select c.id, c.email, c.telephone                       
    from customer c                     
    join booking b on b.customer_id = c.id                      
    where to_char(b.date_created, 'YYYY') = '2018'                      
    group by 1, 2, 3                    
),      
active_2019 as 
(
    select c.id, c.email, c.telephone                       
    from customer c                     
    join booking b on b.customer_id = c.id                      
    where to_char(b.date_created, 'YYYY') = '2019'                      
    group by 1, 2, 3                    
),  
active_2020 as 
(
    select c.id, c.email, c.telephone                       
    from customer c                     
    join booking b on b.customer_id = c.id                      
    where to_char(b.date_created, 'YYYY') = '2020'                      
    group by 1, 2, 3                    
),
active_2021 as 
(
    select c.id, c.email, c.telephone                       
    from customer c                     
    join booking b on b.customer_id = c.id                      
    where to_char(b.date_created, 'YYYY') = '2021'                      
    group by 1, 2, 3                    
)

我意识到以下尝试是错误的,但到目前为止这是我最好的尝试,我认为这个查询是说显示在 2018 年和 2019 年和 2020 年而不是在 2021 年预订的客户预订。这是错误的,因为我实际上需要那些已在 2018 年或 2019 年或 2020 年预订,但未在 2021 年预订。

尝试 #1 - 失败:

select distinct(a.id), a.email, a.telephone     
from active_2018 a on a.id = a.id 
join active_2019 b on a.id = b.id   
join active_2020 c on a.id = c.id           
where a.id not in (select id from active_2021)      

【问题讨论】:

    标签: sql amazon-redshift


    【解决方案1】:

    编辑(再次..]):在下面的评论和查看文档之后。你实际上需要一个工会。因为它们可能在 2018 年、2019 年或 2020 年,但不是 2021 年。“或”是我认为你需要工会而不是加入的原因。除了 2021 年之外,不是每一年都预订的人。

    '
     select distinct(a.id) as id, a.email as email, a.telephone as telephone     
     from active_2018
     where a.id not in (select id from active_2021)
     union
     select distinct(b.id) as id, b.email as email, b.telephone as telephone     
     from active_2018 as b
     where b.id not in (select id from active_2021)
     union
     select distinct(c.id) as id, c.email as email, c.telephone as telephone     
     from active_2018 as c
     where c.id not in (select id from active_2021);               
     
    '
    

    【讨论】:

    • 谢谢 Gregory,我最初包含 active_2021 是为了检查他们是否在 2021 年没有预订。如果没有该表,结果数据可能包括在 2021 年预订和未预订的客户。如果客户有2021 年预订我不希望它们包含在结果中
    • 我已经更新了。您是否收到错误或只是错误的数据?
    • 还要注意,Union 应该在本机上提供不同的结果,所以我对 distinct 的使用是针 CPU 使用率。如果不选择 distinct,查询会更快。
    猜你喜欢
    • 1970-01-01
    • 2023-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多