[英]i need to sum sale and QTy for each DEP between to dates in 2 different column
Date Dep Sales Qty
01/07/2018 0103 13,618 459
20/08/2018 0325 237 13
01/01/2018 0318 2,121 39
30/06/2018 0105 19,886 1,586
01/07/2019 0103 13,618 459
01/04/2019 0325 237 13
01/01/2020 0318 2,121 39
30/06/2020 0105 19,886 1,586
当日期在 (01/07/2018 到 30/06/2019) 列 (18) 之间并且日期在 (01/07/2019 到 30/06/2020) 之间时,我需要为每个 DEP 求和销售额和 QTy在花药柱上 (19)
Dep Sales 18 Sales 19 Qty 18 Qty 19
0103 13,618 13,618 459 459
0325 237 237 13 13
0318 2,121 2,121 39 39
0105 19,886 19,886 1,586 1,586
您可以使用条件聚合:
select dep,
sum(case when date >= '2018-01-01' and date < '2019-01-01' then sales else 0 end) as sales_2018,
sum(case when date >= '2019-01-01' and date < '2020-01-01' then sales else 0 end) as sales_2019,
sum(case when date >= '2018-01-01' and date < '2019-01-01' then qty else 0 end) as qty_2018,
sum(case when date >= '2019-01-01' and date < '2020-01-01' then qty else 0 end) as qty_2019
from t
group by dep;
众所周知,日期操作依赖于数据库。 像这样的东西应该适用于您正在使用的任何数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.