2012-08-28 60 views
1

嗨,我可能過於複雜化我的代碼,所以尋找一個更好的方法。Transact SQL 2總結1查詢使用案例

我有發票表,我比較我在尋找一個簡單的SQL語句,將看日期2個日期範圍是在範圍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 2.2上運行php 5 Mssql 2000(不是由選擇)在apache 2.2上。

感謝所有幫助提供

回答

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 
+0

'SUM(PAID_AMT)和'SUM(PAID_AMT)AS paidTotal2'是相同的。爲什麼讓他們兩次? – Farhan

+0

@MuhammadGhazi你是對的,我剛剛刪除它。我只是複製了最初的請求,卻沒有意識到它們是一樣的。 – Taryn

+0

@bluefeet謝謝你是真棒我已經撞了我的頭反對鍵盤幾個小時,知道它可能大聲笑:) – deccypher

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的查詢,因爲它已被簡化。

0

你不能使用兩個列在您的CASE WHEN ... THEN THEN SUM(INV_AMOUNT) AS OrderTotal, SUM(PAID_AMT) AS paidTotal

如果我理解你的問題很好,你需要它分裂INV_AMOUNT成兩個值的OrderTotal和OrderTotal2的解決方案。如果你想知道有多少在2011年和2012年separetly是價值,你需要的東西是這樣的:AS paidTotal`

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