用SQL写查询实现三个sumifs函数
我有一张类似这样的桌子:用SQL写查询实现三个sumifs函数,sql,sum,Sql,Sum,我有一张类似这样的桌子: ori acc acc_after date1 date2 date3 1 1 1 20/08/20 21/08/20 25/08/20 1 2 1 22/08/20 22/08/20 26/08/20 2 1 3 22/08/20 23/08/20 25/08/20 =SUMIFS(ori;d
ori acc acc_after date1 date2 date3
1 1 1 20/08/20 21/08/20 25/08/20
1 2 1 22/08/20 22/08/20 26/08/20
2 1 3 22/08/20 23/08/20 25/08/20
=SUMIFS(ori;date1<=21/08/20)+SUMIFS(acc;date2<=21/08/20)+SUMIFS(acc_after;date3<=21/08/20)
我想将它们相加为一个速率,excel中的sumifs函数如下所示:
ori acc acc_after date1 date2 date3
1 1 1 20/08/20 21/08/20 25/08/20
1 2 1 22/08/20 22/08/20 26/08/20
2 1 3 22/08/20 23/08/20 25/08/20
=SUMIFS(ori;date1<=21/08/20)+SUMIFS(acc;date2<=21/08/20)+SUMIFS(acc_after;date3<=21/08/20)
=SUMIFS(ori;date1您可以尝试以下方法-
select sum(ori)+sum(acc)+sum(acc_after)
from public.table
where date1 <= '21/08/20'
选择sum(ori)+sum(acc)+sum(acc\u after)
从public.table
where date1我想您需要:
select (sum(case when date1 <= '2020-08-21' then ori else 0 end) +
sum(case when date2 <= '2020-08-21' then acc else 0 end) +
sum(case when date3 <= '2020-08-21' then acc_after else 0 end) +
)
from public.table ;
select(sum(date1时的大小写使用case
表达式进行条件聚合。在标准SQL中,您可以使用sum(…)过滤器(其中…
)-您使用的是哪种DBMS产品?(“SQL”只是所有关系数据库使用的查询语言,而不是特定数据库的名称)谢谢,但在这种情况下,日期列也不同