=>背景介绍
->tableSales表
->第一次查询(2013销售员销售总额)
select salesname,sum(extendedPrice) as sumprice13 from tableSales where salesTime between '2013-01-01' and '2013-12-31' group by salesname
->第二次查询(2014销售员销售总额)
select salesname,sum(extendedPrice) as sumprice14 from tableSales where salesTime between '2014-01-01' and '2014-12-31' group by salesname
->如何获得销售员13、14年的销售总额?
A、首先,我想到的就是用union all,但不理想;
select salesname,sum(extendedPrice) as sumprice13,null as sumprice14 from tableSales where salesTime between '2013-01-01' and '2013-12-31' group by salesname
union all
select salesname,null as sumprice13,sum(extendedPrice) as sumprice14 from tableSales where salesTime between '2014-01-01' and '2014-12-31' group by salesname
B最后,通过fredrickhu版主大大的指点,修改如下:
SELECT
a.salesname,a.sumprice13,b.sumprice14
FROM
(SELECT
salesname,SUM(extendedPrice) AS sumprice13
FROM
tableSales
WHERE
salesTime BETWEEN '2013-01-01' AND '2013-12-31'
GROUP BY
salesname
) AS a
INNER JOIN
(SELECT
salesname,SUM(extendedPrice) AS sumprice14
FROM
tableSales
WHERE
salesTime BETWEEN '2014-01-01' AND '2014-12-31'
GROUP BY
salesname
) AS b
ON a.salesname=b.salesname
最后再次感谢fredrickhu大大!谢谢!..风雨兼程...
总结以上是内存溢出为你收集整理的SqlServer_select-多次sum查询结果合并;全部内容,希望文章能够帮你解决SqlServer_select-多次sum查询结果合并;所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)