SQL UNION ALL 语句
selectconvert(varchar(10),rq,120),sl,jefromkcunionallselectconvert(varchar(7),rq,120)...
select convert(varchar(10),rq,120),sl,je from kc
union all
select convert(varchar(7),rq,120)+N'月合计',sum(sl),sum(je) from kc
group by convert(varchar(7),rq,120)+N'月合计'
运行的结果如截图,我想要的结果是:10月份的记录之后显示,合计数量,
大侠,能解这个的,也帮我解下另一个,我提的问题:SQL 库存明细账月份累计与合计, 可以拿2个问题的分哦
一楼的高手能不能帮我加一项当月累计,就是月合计下面在出现一项累计之前的数据,万分感谢,我想照你的做,没成功 展开
union all
select convert(varchar(7),rq,120)+N'月合计',sum(sl),sum(je) from kc
group by convert(varchar(7),rq,120)+N'月合计'
运行的结果如截图,我想要的结果是:10月份的记录之后显示,合计数量,
大侠,能解这个的,也帮我解下另一个,我提的问题:SQL 库存明细账月份累计与合计, 可以拿2个问题的分哦
一楼的高手能不能帮我加一项当月累计,就是月合计下面在出现一项累计之前的数据,万分感谢,我想照你的做,没成功 展开
1个回答
展开全部
select convert(varchar(10),rq,120) as ddate,sl,je from kc
UNION ALL
SELECT ddate + '月累计',sl,je
FROM
(SELECT kk.ddate,
(SELECT SUM(kk2.sl) FROM (SELECT convert(varchar(7),rq,120) as ddate,SUM(sl) AS sl,SUM(je) AS je FROM kc
GROUP BY convert(varchar(7),rq,120)) kk2 WHERE kk2.ddate <= kk.ddate) AS sl,
(SELECT SUM(kk3.je) FROM (SELECT convert(varchar(7),rq,120) as ddate,SUM(sl) AS sl,SUM(je) AS je FROM kc
GROUP BY convert(varchar(7),rq,120)) kk3 WHERE kk3.ddate <= kk.ddate) AS je
FROM
(SELECT convert(varchar(7),rq,120) as ddate,SUM(sl) AS sl,SUM(je) AS je FROM kc
GROUP BY convert(varchar(7),rq,120)
) kk)
mm
union all
select convert(varchar(7),rq,120)+N'月合计' as ddate,sum(sl),sum(je) from kc
group by convert(varchar(7),rq,120)+N'月合计'
ORDER BY ddate
UNION ALL
SELECT ddate + '月累计',sl,je
FROM
(SELECT kk.ddate,
(SELECT SUM(kk2.sl) FROM (SELECT convert(varchar(7),rq,120) as ddate,SUM(sl) AS sl,SUM(je) AS je FROM kc
GROUP BY convert(varchar(7),rq,120)) kk2 WHERE kk2.ddate <= kk.ddate) AS sl,
(SELECT SUM(kk3.je) FROM (SELECT convert(varchar(7),rq,120) as ddate,SUM(sl) AS sl,SUM(je) AS je FROM kc
GROUP BY convert(varchar(7),rq,120)) kk3 WHERE kk3.ddate <= kk.ddate) AS je
FROM
(SELECT convert(varchar(7),rq,120) as ddate,SUM(sl) AS sl,SUM(je) AS je FROM kc
GROUP BY convert(varchar(7),rq,120)
) kk)
mm
union all
select convert(varchar(7),rq,120)+N'月合计' as ddate,sum(sl),sum(je) from kc
group by convert(varchar(7),rq,120)+N'月合计'
ORDER BY ddate
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询