我有以下数据:
ID ActionType Quantity Fee
1 Commission 1 10
1 Open 2 10
2 Commission 1 20
2 Close 3 20
3 Commission 1 30
3 Transfer 4 30
Run Code Online (Sandbox Code Playgroud)
如何按ID分组并选择不是佣金的ActionType?
所以我想结束
ID ActionType Quantity Fee
1 Open 3 20
2 Close 4 40
3 Transfer 5 60
Run Code Online (Sandbox Code Playgroud)
我可以通过使用ID除以分区来计算出数量和费用的总和,但是我无法弄清楚如何选择不是佣金的对的ActionType.
试试这个:
select id,
max(case
when actiontype = 'Commision'
then null
else actiontype
end) actiontype,
sum(quantity) quantity,
sum(fee) fee
from t
group by id;
Run Code Online (Sandbox Code Playgroud)
除了Commision之外,它需要最多的所有actionType以及其他总和.
| 归档时间: |
|
| 查看次数: |
87 次 |
| 最近记录: |