基本上,我有一個表格,說明與帳戶的通信型別(通過信件或電子郵件),例如:
| ACOUNT_ID | TYPE_OF_COMM |
|---|---|
| 1 | 信 |
| 1 | 信 |
| 3 | 電子郵件 |
| 2 | 信 |
| 4 | 電子郵件 |
| 4 | 信 |
等等..需要撰寫一個查詢:
- 只收到信件的賬戶數量,
- 僅接收郵件的帳戶數量,
- 同時收到信件和郵件的賬戶數量
所以上面的輸出應該是:
| Type_of_comms | 賬戶數 |
|---|---|
| 僅限字母 | 2 |
| 僅電子郵件 | 1 |
| 信件和郵件 | 1 |
提前致謝!
uj5u.com熱心網友回復:
我不熟悉 Athena,但只要它支持 CTE / 子查詢,這應該可以作業:
--if CTE not supported, just change to a subquery
with comm_type as
(
select distinct t1.ACCOUNT_ID
, case
when t1.TYPE_OF_COMM like 'Email' and t2.TYPE_OF_COMM is null then 'Email Only'
when t1.TYPE_OF_COMM like 'Letter' and t2.TYPE_OF_COMM is null then 'Letter Only'
when t1.TYPE_OF_COMM is not null and t2.TYPE_OF_COMM is not null then 'Both Letters and Mails'
else 'Unkown'
end TYPE_OF_COMMS
from tbl t1
left join tbl t2
on t1.ACCOUNT_ID = t2.ACCOUNT_ID
and t1.TYPE_OF_COMM <> t2.TYPE_OF_COMM
)
select TYPE_OF_COMMS
, count(distinct ACCOUNT_ID)
from comm_type
group by TYPE_OF_COMMS
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/492355.html
