Select Case Statement中的聚合总计

时间:2017-06-26 13:34:39

标签: sql-server-2008

有没有办法从每个select case语句中的聚合中获取总数?以下通过列出一个列中每个月的总数给出了正确的总数,但我希望每个案例陈述都有一个总数。

SELECT SUM(
           CASE WHEN dbo.bill_t_ARTransaction.TransactionDate BETWEEN '2000-01-01' AND '2016-12-31' THEN dbo.bill_t_ARTransaction.Amount ELSE 0 END
           ) AS 'Dec16'
        , SUM(
              CASE WHEN dbo.bill_t_ARTransaction.TransactionDate BETWEEN '2000-01-01' AND '2017-01-31' THEN dbo.bill_t_ARTransaction.Amount ELSE 0 END
              ) AS 'JAN17'
FROM dbo.bill_t_ARTransaction
INNER JOIN dbo.bill_t_TripTicket ON (
                                     dbo.bill_t_ARTransaction.RunNumber = dbo.bill_t_TripTicket.RunNumber
                                     )
INNER JOIN dbo.med_m_Company     ON (
                                     dbo.bill_t_TripTicket.CompanyCode = dbo.med_m_Company.CompanyCode
                                     )
WHERE dbo.bill_t_TripTicket.CompanyCode = '105'
  AND dbo.bill_t_ARTransaction.TransactionDate BETWEEN '2000-01-01' AND '2017-01-31'
GROUP BY dbo.bill_t_ARTransaction.TransactionDate

3 个答案:

答案 0 :(得分:0)

我刚刚编写了你的​​SQL - 所以它对人类来说更具可读性 - 并把它放在Subselect中。
在外部选择我刚刚添加了两个计算列的SUM

SELECT sums.*
      ,SUM(sums.DEC16 + sums.JAN17) AS TOTAL_SUM
  FROM (     
            SELECT SUM( CASE WHEN dbo.bill_t_ARTransaction.TransactionDate 
                                  BETWEEN '2000-01-01' 
                                      AND '2016-12-31' 
                             THEN dbo.bill_t_ARTransaction.Amount 
                             ELSE 0 
                        END
                       ) AS DEC16
                   ,SUM( CASE WHEN dbo.bill_t_ARTransaction.TransactionDate 
                                   BETWEEN '2000-01-01' 
                                       AND '2017-01-31' 
                              THEN dbo.bill_t_ARTransaction.Amount 
                              ELSE 0 
                         END
                       ) AS JAN17
             FROM dbo.bill_t_ARTransaction
            INNER JOIN dbo.bill_t_TripTicket 
               ON dbo.bill_t_ARTransaction.RunNumber = dbo.bill_t_TripTicket.RunNumber
            INNER JOIN dbo.med_m_Company     
               ON dbo.bill_t_TripTicket.CompanyCode  = dbo.med_m_Company.CompanyCode
            WHERE dbo.bill_t_TripTicket.CompanyCode        = '105'
              AND dbo.bill_t_ARTransaction.TransactionDate BETWEEN '2000-01-01' AND '2017-01-31'
            GROUP BY dbo.bill_t_ARTransaction.TransactionDate
        ) sums

答案 1 :(得分:0)

你可以这样做:

SELECT  DATENAME(MONTH, Transaction.TransactionDate) + RIGHT(YEAR(Transaction.TransactionDate), 2) AS MonthYear
,       SUM(Transaction.Amount) AS Amount
FROM    dbo.bill_t_ARTransaction Transaction
INNER JOIN dbo.bill_t_TripTicket Ticket
    ON Transaction.RunNumber = Ticket.RunNumber 
INNER JOIN dbo.med_m_Company Company
    ON Ticket.CompanyCode = Company.CompanyCode 
WHERE   Ticket.CompanyCode = '105'
        AND Transaction.TransactionDate >= '2000-01-01' 
        AND Transaction.TransactionDate < '2017-02-01'
GROUP BY DATENAME(MONTH, Transaction.TransactionDate) + RIGHT(YEAR(Transaction.TransactionDate), 2)

假设dbo.bill_t_ARTransaction.TransactionDatedatetime(2):您是否知道在'DEC16'中您缺少来自(作为示例)2016-12-31 11:00:00的交易? BETWEEN包含双方,如果未定义任何时间组件,则日期默认为午夜(00:00:00)。我相应地修改了WHERE条款。我还添加了别名来帮助提高可读性。

答案 2 :(得分:0)

我认为你只想改进GROUP BY

SELECT SUM(CASE WHEN ba.TransactionDate BETWEEN '2000-01-01' AND '2016-12-31'
               THEN ba.Amount ELSE 0
           END) AS Dec16,
       SUM(CASE WHEN ba.TransactionDate BETWEEN '2000-01-01' AND '2017-01-31'
                THEN ba.Amount ELSE 0
            END) AS JAN17
FROM dbo.bill_t_ARTransaction ba INNER JOIN
     dbo.bill_t_TripTicket bt
     ON ba.RunNumber = bt.RunNumber INNER JOIN
     dbo.med_m_Company c
     ON bt.CompanyCode = c.CompanyCode
WHERE bt.CompanyCode = '105' AND
      ba.TransactionDate BETWEEN '2000-01-01' AND '2017-01-31';

请注意我对查询所做的其他更改:

  • 我从列别名中删除了单引号。单引号应仅用于字符串和日期值(允许使用它们用于列别名但可能会导致混淆)。
  • 表中给出了别名。
  • 列名用别名限定(查询更容易编写和阅读。
  • 请注意,如果'105'为数字,CompanyCode不应包含引号。

我认为查询可以简化为:

SELECT SUM(CASE WHEN ba.TransactionDate BETWEEN '2000-01-01' AND '2016-12-31'
               THEN ba.Amount ELSE 0
           END) AS Dec16,
       SUM(CASE WHEN ba.TransactionDate BETWEEN '2000-01-01' AND '2017-01-31'
                THEN ba.Amount ELSE 0
            END) AS JAN17
FROM dbo.bill_t_ARTransaction ba INNER JOIN
     dbo.bill_t_TripTicket bt
     ON ba.RunNumber = bt.RunNumber 
WHERE bt.CompanyCode = 105 AND
      ba.TransactionDate BETWEEN '2000-01-01' AND '2017-01-31';

Company表似乎没有被使用。