2011-01-26 68 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来实现你想要的。

过了我的睡前时间,如果别人没有打败我,我明天就会明白它的意思,但是我以为我会给你方向让你进入。