select a.month_id, sum(b.aa) aa, sum(b.bb) bb, sum(b.cc) cc, '今年往月新增订购' as A from ( select month_id, sum(aa) aa,sum(bb) bb,sum(cc) cc from ( select month_id , count(distinct case when offer_type_name='主套餐' then offer_name end) aa, count(distinct case when offer_type_name='促销' then offer_name end) bb, count(distinct case when offer_type_name='可选包' then offer_name end) cc, from dwd_sum_zhuliuxxsc where substring(crt_version,1,4)='2023' group by month_id ) ptuser group by month_id ) a join ( select month_id,sum(aa) aa,sum(bb) bb,sum(cc) cc from ( select month_id , count(distinct case when offer_type_name='主套餐' then offer_name end) aa, count(distinct case when offer_type_name='促销' then offer_name end) bb, count(distinct case when offer_type_name='可选包' then offer_name end) cc, from dwd_sum_zhuliuxxsc where substring(crt_version,1,4)='2023' group by month_id ) ptuser group by month_id ) b on a.month_id >= b.month_id group by a.month_id order by a.month_id;如何改成计算今年往月(不包含本月)的统计
时间: 2024-02-02 07:03:17 浏览: 105
LINQ_to_SQL.zip_SUM_linq
您可以将子查询中的 `where substring(crt_version,1,4)='2023'` 改为 `where year(crt_version) = year(current_date()) and month_id < month(current_date())`,这样就只会统计今年往月的数据(不包含本月)。修改后的 SQL 语句如下:
```
select a.month_id,
sum(b.aa) aa,
sum(b.bb) bb,
sum(b.cc) cc,
'今年往月新增订购' as A
from (
select month_id, sum(aa) aa,sum(bb) bb,sum(cc) cc
from (
select month_id,
count(distinct case when offer_type_name='主套餐' then offer_name end) aa,
count(distinct case when offer_type_name='促销' then offer_name end) bb,
count(distinct case when offer_type_name='可选包' then offer_name end) cc
from dwd_sum_zhuliuxxsc
where year(crt_version) = year(current_date()) and month_id < month(current_date())
group by month_id
) ptuser
group by month_id
) a
join (
select month_id,sum(aa) aa,sum(bb) bb,sum(cc) cc
from (
select month_id,
count(distinct case when offer_type_name='主套餐' then offer_name end) aa,
count(distinct case when offer_type_name='促销' then offer_name end) bb,
count(distinct case when offer_type_name='可选包' then offer_name end) cc
from dwd_sum_zhuliuxxsc
where year(crt_version) = year(current_date()) and month_id < month(current_date())
group by month_id
) ptuser
group by month_id
) b
on a.month_id >= b.month_id
group by a.month_id
order by a.month_id;
```
注意,这里的 `current_date()` 函数是 MySQL 中获取当前日期的函数,如果您使用的是其他数据库,获取当前日期的函数可能有所不同。
阅读全文