数据选择/行选择

问题描述 投票:0回答:1

我有一种情况,我必须编写一个仅考虑负值的SQL查询

[输出]

enter image description here

如何实现输出

sql amazon-redshift partitioning
1个回答
0
投票

您似乎正在寻找条件sum。逻辑是将case表达式放在聚合函数中,如下所示:

select
    sum(case when "2020" < 0 then "2020" else 0 end)  result_2020,
    sum(case when "2021" < 0 then "2021" else 0 end)  result_2021
from mytable
© www.soinside.com 2019 - 2024. All rights reserved.