Oracle查询显示来自同一列的2个求和字段,并根据不同表的不同字段条件进行计算

时间:2017-09-15 17:08:05

标签: sql oracle

我有2张桌子以下,我必须写一个查询来显示通过信用卡收到的总金额的总和并检查。将别名分别命名为“total_credit”和“total_cheque”。

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,请提供建议。

6 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;

答案 2 :(得分:0)

setTimeout(function(){
  document.getElementById('width-controller').style.width = '400px';
},200);

我考虑将它用作两个子查询,但看起来我们没有加入任何其他ID或任何东西。

完全披露:我不熟悉Oracle,但这个查询肯定会在T-SQL中运行,我相信它可以在MySQL中运行,所以我不明白为什么它在Oracle中也不起作用。

答案 3 :(得分: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

答案 4 :(得分:0)

不按tt.type分组 - 这就是您获得该结果的原因。如果您需要按user_id分组(如在其中一个解决方案中),那么就可以;如果您需要一个只有一行和两列的结果,对所有用户进行聚合(求和),不按任何分组,则不需要GROUP BY子句。 (我相信SQL标准不允许这样做; Oracle肯定会这样做,但如果这让你烦恼,你可以添加一些微不足道的东西,比如GROUP BY NULL。)

答案 5 :(得分:0)

仅使用没有group by子句的代码

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