2017-09-15 39 views
1

我有2個以下的表格,我必須寫一個查詢來顯示通過信用卡接收的總金額的總和並檢查。將別名分別命名爲'total_credit'和'total_cheque'。Oracle查詢顯示來自同一列的2個總和字段,並根據不同表格中的不同字段條件計算出來

table_payment
ID(PK)
USER_ID

transaction_type_id(FK)

table_transaction_type
ID(PK)

預期輸出:

total_credit| total_cheque  
Value a  | Value B 

我試過下面的查詢,但我在不同的行

select sum(case when tt.type = 'Credit Card' then pt.amount end) as 
"total_credit", sum (case when tt.type = 'Cheque' then pt.amount end) 
as "total_cheque" from payment pt join transaction_type tt on 
pt.transaction_type_id = tt.id group by tt.type 

Actual_Output獲取值:

total_credit| total_cheque  
Value A  |  
      | Value B 

不知道如何在第一行獲得B值A值一起,請指教。

回答

0
select user_id, sum(case when tt.type='Credit Card' the amount end) as total_credit, sum(case when tt.type='Cheque' the amount end) as total_credit 
from table_payment p 
inner join table_transaction_type tt 
on p.transaction_type_id = tt.id 
group by user_id; 
+1

看起來你決定改變表名('table_cheque'不是表在他原來的問題) – phroureo

+0

@phroureo感謝您的評論,更正! –

0
select (SELECT sum(pt.amount FROM from payment pt 
join transaction_type tt on pt.transaction_type_id = tt.id 
WHERE tt.type = 'Credit Card' 
group by tt.type) as "total_credit" 
, (SELECT sum(pt.amount FROM from payment pt 
join transaction_type tt on pt.transaction_type_id = tt.id 
WHERE tt.type = 'Cheque' 
group by tt.type) as "total_cheque" 

我認爲用它作爲兩個子查詢,但看起來我們沒有任何其他ID或任何連接。我不熟悉Oracle,但是這個查詢肯定會在T-SQL中工作,我相信它可以在MySQL中工作,所以我不明白爲什麼它在Oracle中不起作用,太。

0

難道你不能只給它增加一個最大值?

SELECT MAX (SUM (CASE WHEN tt.TYPE = 'Credit Card' THEN pt.amount END)) 
      AS "total_credit", 
     MAX (SUM (CASE WHEN tt.TYPE = 'Cheque' THEN pt.amount END)) 
      AS "total_cheque" 
    FROM payment pt JOIN transaction_type tt ON pt.transaction_type_id = tt.id 
GROUP BY tt.TYPE 

這裏是一個假數據的例子,你可以在oracle中運行。

WITH 
    transaction_type 
    AS 
     (SELECT 1 id, 'Credit Card' AS TYPE FROM DUAL 
     UNION ALL 
     SELECT 2, 'Cheque' FROM DUAL), 
    payment 
    AS 
     (SELECT 500 AS amount, 1 AS transaction_type_id FROM DUAL 
     UNION ALL 
     SELECT 300, 2 FROM DUAL 
     UNION ALL 
     SELECT 200, 1 FROM DUAL 
     UNION ALL 
     SELECT 400, 2 FROM DUAL) 
    SELECT MAX (SUM (CASE WHEN tt.TYPE = 'Credit Card' THEN pt.amount END)) 
      AS "total_credit", 
     MAX (SUM (CASE WHEN tt.TYPE = 'Cheque' THEN pt.amount END)) 
      AS "total_cheque" 
    FROM payment pt JOIN transaction_type tt ON pt.transaction_type_id = tt.id 
GROUP BY tt.TYPE 

結果

total_credit total_cheque 
700   700 
+1

太複雜了!只需在原始查詢中刪除GROUP BY子句 - 這就是所需要的。 – mathguy

+0

我是SUM而不是MAX需要什麼? –

0

不組由tt.type - 這就是爲什麼你得到這一結果。如果您需要按照user_id進行分組(如在其中一種解決方案中),那麼請執行;如果您需要結果只有一行兩列,則對所有用戶進行彙總(彙總),不要按任何組合,您不需要GROUP BY子句。 (我相信SQL標準不允許這樣; Oracle肯定會這樣做,但是如果這樣做會困擾你,你可以添加一些無關緊要的東西,比如GROUP BY NULL。)

1

你想要做的是「pivot」數據和Oracle提供了旋轉功能:

WITH 
    T 
AS 
(
    select tt.type, pt.amount 
    from payment pt 
    join transaction_type tt on tt.id = pt.transaction_type_id 
) 
SELECT 
    * 
FROM 
    T 
PIVOT 
(
    SUM(amount) 
    FOR 
     (type) 
    IN 
     ('Credit Card', 'Cheque') 
); 

進一步的例子可以在這裏找到:http://www.dba-oracle.com/t_pivot_examples.htm

相關問題