使用case进行Transact SQL 2 sum 1查询

时间:2012-08-28 16:38:45

标签: sql

我可能过度复杂化我的代码,因此寻找更好的方法。

我有一个发票表,我正在比较2个日期范围我正在寻找一个简单的sql语句,它将查看日期在范围1中添加到ordertotal1否则如果在日期范围2中添加到ordertotal2

这是我的尝试

    SELECT     CUST_CODE,

    CASE
        WHEN  ORDER_DATE >= CONVERT(DATETIME, '2011-01-01 00:00:00', 102) AND ORDER_DATE <= CONVERT(DATETIME, '2011-07-31 23:59:59', 102) THEN  SUM(INV_AMOUNT) AS OrderTotal, SUM(PAID_AMT) AS paidTotal
WHEN  ORDER_DATE >= CONVERT(DATETIME, '2012-01-01 00:00:00', 102) AND ORDER_DATE <= CONVERT(DATETIME, '2012-07-31 23:59:59', 102)  THEN SUM(INV_AMOUNT) AS OrderTotal2, SUM(PAID_AMT) AS paidTotal2
    END
    FROM         INVOICES
    WHERE (STATUS = 9)
    GROUP BY CUST_CODE

当然这确实是错误的并且引发了很多错误, 我想我注定要在我的php中运行几个查询来实现它,但我确信我记得过去做过类似的事情。

仅供参考:在apache 2.2 for windows上运行php 5 Mssql 2000(不是选择)。

感谢您提供的任何帮助

3 个答案:

答案 0 :(得分:1)

您的CASE声明似乎缺少END

SELECT CUST_CODE,
    CASE
        WHEN  ORDER_DATE >= CONVERT(DATETIME, '2011-01-01 00:00:00', 102) 
            AND ORDER_DATE <= CONVERT(DATETIME, '2011-07-31 23:59:59', 102) 
        THEN  SUM(INV_AMOUNT) END AS OrderTotal, 
        SUM(PAID_AMT) AS paidTotal,
    CASE
        WHEN  ORDER_DATE >= CONVERT(DATETIME, '2012-01-01 00:00:00', 102) 
            AND ORDER_DATE <= CONVERT(DATETIME, '2012-07-31 23:59:59', 102)  
        THEN SUM(INV_AMOUNT) END AS OrderTotal2 
    ---SUM(PAID_AMT) AS paidTotal2 not needed
FROM         INVOICES
WHERE (STATUS = 9)
GROUP BY CUST_CODE

或者更好:

SELECT CUST_CODE,
    SUM(
        CASE
        WHEN  ORDER_DATE >= CONVERT(DATETIME, '2011-01-01 00:00:00', 102) 
            AND ORDER_DATE <= CONVERT(DATETIME, '2011-07-31 23:59:59', 102) 
        THEN  INV_AMOUNT END) AS OrderTotal, 
        SUM(PAID_AMT) AS paidTotal,
    SUM(
        CASE
        WHEN  ORDER_DATE >= CONVERT(DATETIME, '2012-01-01 00:00:00', 102) 
            AND ORDER_DATE <= CONVERT(DATETIME, '2012-07-31 23:59:59', 102)  
        THEN INV_AMOUNT END ) AS OrderTotal2 
    ---SUM(PAID_AMT) AS paidTotal2 not needed
FROM         INVOICES
WHERE (STATUS = 9)
GROUP BY CUST_CODE

答案 1 :(得分:0)

ORDER_DATE的数据类型究竟是什么?你为什么不使用CAST:

cast('2011-01-01 00:00:00' as DATETIME)

无论如何,请将此作为两个CASE条款:

case
    when ORDER_DATE between cast('2011-01-01 00:00:00' as datetime) and cast('2011-07-31 23:59:59' as datetime)
    then...
end

对于其余部分,您可以使用bluefeet的查询,因为它已被简化。

答案 2 :(得分:0)

您无法在CASE WHEN ... THEN THEN SUM(INV_AMOUNT) AS OrderTotal, SUM(PAID_AMT) AS paidTotal

中使用两列

如果我理解你的问题,你需要一个解决方案,将INV_AMOUNT分成两个值OrderTotal和OrderTotal2。如果你想知道2011年和2012年的价值是多少,你需要这样的东西:

 SELECT CUST_CODE,
        SUM(CASE
            WHEN  ORDER_DATE >= CONVERT(DATETIME, '2011-01-01 00:00:00', 102) 
                AND ORDER_DATE <= CONVERT(DATETIME, '2011-07-31 23:59:59', 102) 
            THEN  INV_AMOUNT ELSE 0 END) AS OrderTotal, 
            SUM(PAID_AMT) AS paidTotal,
        SUM(CASE
            WHEN  ORDER_DATE >= CONVERT(DATETIME, '2012-01-01 00:00:00', 102) 
                AND ORDER_DATE <= CONVERT(DATETIME, '2012-07-31 23:59:59', 102)  
            THEN INV_AMOUNT ELSE 0 END) AS OrderTotal2, 
        SUM(PAID_AMT) AS paidTotal2
    FROM         INVOICES
    WHERE (STATUS = 9)
    GROUP BY CUST_CODE