1

嗨,我可能会使我的代码过于复杂,因此正在寻找更好的方法。

我有一个发票表,我正在比较 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 中运行几个查询来实现它,但我确定我记得过去做过类似上面的事情。

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

感谢您提供的任何帮助

4

3 回答 3

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
于 2012-08-28T16:44:16.703 回答
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 的查询,因为它已被简化。

于 2012-08-28T16:59:02.540 回答
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
于 2012-08-28T16:59:13.683 回答