上个题我已经给过你提示了
sql判断某一列,加载列的和-微软技术-CSDN问答
CSDN问答为您找到sql判断某一列,加载列的和相关问题答案,如果想了解更多关于sql判断某一列,加载列的和 c#、后端、数据库架构 技术问题等相关问答,请访问CSDN问答。
https://ask.csdn.net/questions/7678158?spm=1001.2014.3001.5505
下面是这个sql就是按照我上个题里的思路写的应该符合你的要求
select * from (
select t.jh,mth,sum(ryou_sum) over(partition by jh order by mth) ryou_sum1 from
(select jh,month(rq) mth,sum(ryou) ryou_sum from CSJING_SCSJB
where rq >='2022-01-01' and rq<='2022-12-31' group by month(rq),jh) t) tt
pivot (max(ryou_sum1) for mth in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) as x
sqlserver里实测效果
不知道你要不要按jh分组,如果不分组,把jh相关的去掉就行了
没想到是这么老的版本,用下面这个吧
select jh,
case when sum(case when D.RQ BETWEEN '2022-01-01' and '2022-01-31' then d.ryou end) is null then null
else sum(case when D.RQ BETWEEN '2022-01-01' and '2022-01-31' then d.ryou end) end m1,
case when sum(case when D.RQ BETWEEN '2022-02-01' and '2022-02-28' then d.ryou end) is null then null
else sum(case when D.RQ BETWEEN '2022-01-01' and '2022-02-28' then d.ryou end) end m2,
case when sum(case when D.RQ BETWEEN '2022-03-01' and '2022-03-31' then d.ryou end) is null then null
else sum(case when D.RQ BETWEEN '2022-01-01' and '2022-03-31' then d.ryou end) end m3,
case when sum(case when D.RQ BETWEEN '2022-04-01' and '2022-04-30' then d.ryou end) is null then null
else sum(case when D.RQ BETWEEN '2022-01-01' and '2022-04-30' then d.ryou end) end m4
from CSJING_SCSJB d group by jh