根据当天查找总日销售额

时间:2013-05-29 04:41:14

标签: sql sql-server

我在Sale表中有以下数据:

   Staff_Code  | Tran_Date  |  Morning_Sale | Night_Sale | Total_Sale
       S01     | 2013-05-01 |     50.00     |   300.00   |  350.00
       S02     | 2013-05-01 |     10.00     |   70.00    |   80.00
       S01     | 2013-05-02 |     100.00    |   20.00    |  120.00

我想再添加1列“total_Day_Sale”来了解每日总销售情况,格式如下表所示:

   Staff_Code  | Tran_Date  |  Morning_Sale | Night_Sale | Total_Sale | Total_Day_Sale
       S01     | 2013-05-01 |     50.00     |   300.00   |  350.00    |   430.00  
       S02     | 2013-05-01 |     10.00     |   70.00    |   80.00    |   430.00
       S01     | 2013-05-02 |     100.00    |   20.00    |  120.00    |   120.00

我正在尝试以下代码但不是我预期的结果!

SELECT Tran_Date,Morning_Sale,Night_Sale,Morning_Sale + Night_Sale As 'Total_Sale', 
sum(Morning_Sale)+sum(Night_Sale ) As 'Total_Day_Sale' FROM Sale 
WHERE Tran_Date=Tran_Date  

有可能吗?感谢

5 个答案:

答案 0 :(得分:2)

您可以在相关子查询中执行此操作,

SELECT  Tran_Date,
        Morning_Sale,
        Night_Sale,
        Morning_Sale + Night_Sale As 'Total_Sale', 
        (
            SELECT  SUM(Morning_Sale + Night_Sale)
            FROM    Sale b
            WHERE   a.Tran_Date = b.Tran_Date AND
                    b.Tran_Date = _Tran_Date
        )   As 'Total_Day_Sale' 
FROM    Sale a
WHERE   Tran_Date = _Tran_Date

使用JOIN

SELECT  a.Tran_Date,
        a.Morning_Sale,
        a.Night_Sale,
        a.Morning_Sale + a.Night_Sale As 'Total_Sale', 
        b.Total_Day_Sale
FROM    Sale a
        INNER JOIN
        (
            SELECT  Tran_Date, SUM(Morning_Sale + Night_Sale) Total_Day_Sale
            FROM    Sale 
            WHERE   Tran_Date = _Tran_Date
            GROUP   BY Tran_Date
        ) b   ON a.Tran_Date = b.Tran_Date
WHERE   a.Tran_Date = _Tran_Date

作为建议,参数名称应与列名称不同。

答案 1 :(得分:1)

不要存储它,而是使用视图,然后将表与视图连接。如果您使用的是SQL Server,则可以使用CTE而不是视图:

WITH DailyTotal AS
(SELECT Tran_Date, sum(Morning_Sale + Night_Sale) AS "Total_Day_Sale"
FROM Sale
GROUP BY Tran_Date)
SELECT Sale.Tran_Date, Morning_Sale, Night_Sale,
       Morning_Sale + Night_Sale As "Total_Sale", Total_Day_Sale
FROM Sale, DailyTotal
WHERE Sale.Tran_Date = DailyTotal.Tran_Date;

如果您需要非常高的读取性能,则可能需要实现视图。但从这开始。

答案 2 :(得分:1)

我认为最简单的方法是使用SUM()Over PARTITION BY子句。

SELECT Staff_Code,
        Tran_Date,
        Morning_Sale,
        Night_Sale,
        Total_Sale,
SUM(Total_Sale)  OVER (PARTITION BY Tran_Date) AS Total_Day_Sale
FROM #DailyTotals

输出:

Staff_Code Tran_Date  Morning_Sale Night_Sale  Total_Sale  Total_Day_Sale
---------- ---------- ------------ ----------- ----------- --------------
       S01 2013-05-01 50           300         350         430
       S02 2013-05-01 10           70          80          430
       S01 2013-05-02 100          20          120         120

(3 row(s) affected)

希望这有帮助! (P.S .->因为我使用INT作为临时表的数据类型,因此十进制之后的零消失了。这个查询对你有用,因为你的基础数据已经是十进制/浮点数。

答案 3 :(得分:0)

您可以使用触发器解决此问题。每当输入新值时,触发器将其值添加到Total_Day_Sale。这是什么数据库?

答案 4 :(得分:0)

您可能不希望以这种方式更改表格。每次输入当天的新条目时,都需要您更新每天的每个条目 - 存储新的总计。我会使用另一个表来存储每日总计,每当您添加新的每日条目时,只需要一次更新:

Date       | Total Sale
2013-05-01 | 480.00