2011-01-26 66 views
2

我有以下查詢,它提供了我想要的數據,但是,我需要CASE語句中Cash,Credit和Check列的總和。我怎樣才能做到這一點?如果可能的話,我想使用一個過程。如何獲得列的總數

此外,對我來說,這個查詢似乎並沒有那麼高效。任何人都可以改進這一點?在我看來,我應該能夠設置變量,然後在我的CASE語句中使用它們,但不知道它是如何完成的。

SELECT 
t1.order_id, 
t3.price * SUM(t1.quantity) AS subtotal, 
(SUM(t1.discount) + t3.discount)/100 AS disc, 
t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 AS tax, 
t3.price * SUM(t1.quantity) - (SUM(t1.discount) + t3.discount)/100 + t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 AS total, 
CASE t2.payment_type WHEN 'Cash' THEN t3.price * SUM(t1.quantity) - (SUM(t1.discount) + t3.discount)/100 + t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 ELSE 0.00 END AS cash, 
CASE t2.payment_type WHEN 'Card' THEN t3.price * SUM(t1.quantity) - (SUM(t1.discount) + t3.discount)/100 + t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 ELSE 0.00 END AS card, 
CASE t2.payment_type WHEN 'Check' THEN t3.price * SUM(t1.quantity) - (SUM(t1.discount) + t3.discount)/100 + t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 ELSE 0.00 END AS check 
FROM pos_item_order AS t1 
Join pos_order AS t2 ON t2.order_id = t1.order_id 
Join inv_item AS t3 ON t3.item_id = t1.item_id 
GROUP BY t1.order_id, t1.item_id 

編輯:當我說我「需要的現金,信用卡的總和,並檢查列」,我的意思是每列相應的總數。

+0

`與rollup`做你需要的嗎? – 2011-01-26 07:49:48

+0

嗯..我以前沒有用過。我現在會讀一下。感謝您的建議,馬丁。 – Jim 2011-01-26 07:52:28

+0

@Martin,看起來'彙總'水平合計行,我需要垂直總和或總計。 – Jim 2011-01-26 07:58:40

回答

1

這是一個可怕的查詢,但頭腦簡單的擴展,你已經有給你什麼,我認爲你所要求的:

SELECT t1.order_id, 
     t3.price * SUM(t1.quantity) AS subtotal, 
     (SUM(t1.discount) + t3.discount)/100 AS disc, 
     t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 AS tax, 
     t3.price * SUM(t1.quantity) - (SUM(t1.discount) + t3.discount)/100 + t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 AS total, 
     CASE t2.payment_type WHEN 'Cash' 
     THEN t3.price * SUM(t1.quantity) - (SUM(t1.discount) + t3.discount)/100 + t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 
     ELSE 0.00 END AS cash, 
     CASE t2.payment_type WHEN 'Card' 
     THEN t3.price * SUM(t1.quantity) - (SUM(t1.discount) + t3.discount)/100 + t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 
     ELSE 0.00 END AS card, 
     CASE t2.payment_type WHEN 'Check' 
     THEN t3.price * SUM(t1.quantity) - (SUM(t1.discount) + t3.discount)/100 + t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 
     ELSE 0.00 END AS check, 
     CASE WHEN t2.payment_type IN ('Cash', 'Card', 'Check') 
     THEN t3.price * SUM(t1.quantity) - (SUM(t1.discount) + t3.discount)/100 + t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 ELSE 0.00 END AS cash_card_check 
    FROM pos_item_order AS t1 
    JOIN pos_order AS t2 ON t2.order_id = t1.order_id 
    JOIN inv_item AS t3 ON t3.item_id = t1.item_id 
GROUP BY t1.order_id, t1.item_i 

IN(...)符號可能無法正常工作;如果沒有,你可以寫出一個三路OR條件。但是,我不禁想到必須有更好的方式來構建查詢。

此查詢爲您提供基本的詳細信息,包括付款類型。將其保存到臨時表中,或者如果DBMS支持,則在WITH子句中使用命名的子表達式。

SELECT t1.order_id, 
     t2.payment_type, 
     t3.price * SUM(t1.quantity) AS subtotal, 
     (SUM(t1.discount) + t3.discount)/100 AS disc, 
     t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 AS tax, 
     t3.price * SUM(t1.quantity) - (SUM(t1.discount) + t3.discount)/100 + t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 AS total, 
     t3.price * SUM(t1.quantity) - (SUM(t1.discount) + t3.discount)/100 + t3.price * SUM(t1.quantity) * (t3.tax_state + t3.tax_fed) /100 AS payment 
    FROM pos_item_order AS t1 
    JOIN pos_order  AS t2 ON t2.order_id = t1.order_id 
    JOIN inv_item  AS t3 ON t3.item_id = t1.item_id 
GROUP BY t1.order_id, t1.item_i, t2.payment_type 

然後,您可以分別彙總卡,支票和現金。

0

不幸的是,你不能在另一個表達式中使用派生列,但你可能能夠通過use a subquery來實現你想要的。

過了我的睡前時間,如果別人沒有打敗我,我明天就會明白它的意思,但是我以爲我會給你方向讓你進入。