0
我正在进行一些事务分析。 我希望看到每笔交易数量的交易量。 下面是数据和结果通缉。SQL SUM COUNT CASE
table name: trans_cust
store_id trans_id product qty
A 123 milk 1
A 123 chocolate 1
A 123 candy 1
A 456 milk 4
A 789 candy 1
B 321 chocolate 3
B 321 napkin 1
B 654 blueberry 2
C 987 candy 6
Result
Quantity num_Transaction
1 1
2 1
3 1
4+ 3
我有以下的尝试,但它不工作。
select count(distinct(trans_id)) as num_transation, case
when sum(quantity) = 1 then '1'
when sum(quantity) = 2 then '2'
when sum(quantity) = 3 then '3'
when sum(quantity)>= 5 then '5+' else 'other' end as Quantity
from (select sum(quantity) as Quantity from trans_cust)
请问大家请帮忙吗?谢谢。
似乎是一个派生表是得心应手。 – jarlh
你使用的是什么版本的SQL? –
蜂巢。上表是一个非常简化的版本。谢谢。 – user8448815