正确过滤 table 其中包含同一帐户内的多个记录
Proper filtering of table which contains multiple records within the same account
我需要加入table“客户”和table“评论”。
我想从“客户”table 中找出创建于 2021 年 3 月 24 日的帐户。随后,过滤那些在“评论”table的“comment_desc”栏中没有“休眠”字样的帐户。
参考我的SQL,表格和预期结果:
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
请协助建议我应该如何 revise/improve 我的 SQL。
你可以这样做:
select *
from customer
where creation_date = '03/24/2021'
and account not in (
select account from comment where comment_desc like '%Dormant%'
)
我需要加入table“客户”和table“评论”。 我想从“客户”table 中找出创建于 2021 年 3 月 24 日的帐户。随后,过滤那些在“评论”table的“comment_desc”栏中没有“休眠”字样的帐户。 参考我的SQL,表格和预期结果:
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
请协助建议我应该如何 revise/improve 我的 SQL。
你可以这样做:
select *
from customer
where creation_date = '03/24/2021'
and account not in (
select account from comment where comment_desc like '%Dormant%'
)