【问题标题】:Proper filtering of table which contains multiple records within the same account正确过滤包含同一帐户中的多个记录的表
【发布时间】:2021-06-22 21:09:23
【问题描述】:

我需要加入表格“客户”和表格“评论”。 我想从“客户”表中找出 2021 年 3 月 24 日创建的帐户。随后,在“comment”表的“comment_desc”列中过滤那些没有“休眠”字样的帐户。 参考我的 SQL、Tables 和预期结果:

My SQL:
Select customer.account, customer.creation_date, customer.country 
From DATABASE.customer, DATABASE.comment
Where customer.account=comment.account
And customer.creation_date = '03/24/2021'
And comment.comment_desc not like '%Dormant%'

Table 1: Customer
account | creation_date | country
1111111 | 24/3/2021     | AU
2222222 | 25/3/2021     | NZ
3333333 | 24/3/2021     | AU
4444444 | 24/3/2021     | AU
5555555 | 24/3/2021     | NZ

Table 2: Comment
account | comment_date  | comment_desc
1111111 | 24/3/2021     | newly created
1111111 | 24/3/2021     | callback
1111111 | 25/3/2021     | Update rate
1111111 | 25/3/2021     | Sales follow up
2222222 | 25/3/2021     | newly created
2222222 | 25/3/2021     | Sales follow up
2222222 | 25/3/2021     | Update rate
3333333 | 24/3/2021     | callback
3333333 | 25/3/2021     | manual Dormant
4444444 | 24/3/2021     | Update rate
4444444 | 24/3/2021     | Update account info
4444444 | 24/3/2021     | Sales follow up
5555555 | 24/3/2021     | Update rate
5555555 | 24/3/2021     | Dormant entry

Expected Result:
account  | creation_date | country
1111111  | 24/3/2021     | AU
4444444  | 24/3/2021     | AU

请帮助建议我应该如何修改/改进我的 SQL。

【问题讨论】:

    标签: sql teradata teradatasql


    【解决方案1】:

    你可以这样做:

    select *
    from customer
    where creation_date = '03/24/2021'
      and account not in (
        select account from comment where comment_desc like '%Dormant%'
      )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多