如何获取存储过程中的列总数?

时间:2015-02-22 07:19:41

标签: sql-server-2008

SELECT 
   CONVERT(VARCHAR, b.date, 105)    AS 'Date', 
   m.menucost * (Sum(bm.qty))    AS 'Total' 
FROM   
   billmaster b 
LEFT JOIN 
   billmenumapping bm ON bm.mapbillid = b.billid 
LEFT JOIN 
   menumaster m  ON m.menuid = bm.mapmenuid 
WHERE  
   b.date BETWEEN '2015/02/22' AND '2015/02/25'
GROUP BY
   b.Date, m.menucost

此代码返回以下结果:

Date         Total
-------------------
23-02-2015   40.00
22-02-2015   40.00
22-02-2015   1800.00

我需要输出的方式应该结合日期22-02-2015日期的总和,并且只应给出一个结果,如下所示。总列的总和应根据各自的日期进行。

Date         Total
--------------------
23-02-2015   40.00
22-02-2015   1840.00

请帮忙。

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT Date,SUM(Total) FROM
(SELECT CONVERT(VARCHAR, b.date, 105)    AS 'Date', 
           m.menucost * (Sum(bm.qty))    AS 'Total' 
    FROM   billmaster b 
           LEFT JOIN billmenumapping bm 
                  ON bm.mapbillid = b.billid 
           LEFT JOIN menumaster m 
                  ON m.menuid = bm.mapmenuid 
    WHERE  b.date between '2015/02/22'  and '2015/02/25'
    GROUP BY b.Date,m.menucost) TempTable
GROUP BY Date