简体   繁体   中英

Oracle SQL - 2 totals from the same table

I'm new to Oracle Sql and having a block on how to total amounts by filters on the same table. eg

Employee Table

Emp_ID 
Emp_name 

Transaction File

TR_ID (= Emp_ID )

TR_QTR
TR_Type           
TR_Xref
TR_Amt

EMPLOYEE TABLE

| EMP_ID | EMP_NAME       |
+--------+----------------+
|  1     | sam spade      |
|  2     | junior samples |
|  3     | april showers  |

Transaction Table

| ID | QTR | Type | Xref | Amt   |
+----+-----+------+------+-------+
|  1 |  1  |  W   |  F   | 5.00  |
|  1 |  1  |  W   |  T   | 2.23  |
|  1 |  2  |  W   |  T   | 5.55  |
|  1 |  1  |  W   |  T   | 4.44  |
|  1 |  1  |  W   |  F   | 3.25  |
|  1 |  1  |  G   |  B   | 1.23  |
|  2 |  1  |  W   |  T   | 6.10  |
|  2 |  1  |  G   |  Z   | 12.12 |
|  2 |  1  |  W   |  F   | 9.88  |
|  2 |  1  |  W   |  F   | 8.70  |
|  3 |  1  |  W   |  F   | 4.00  |
|  3 |  1  |  W   |  T   | 3.00  |
|  3 |  1  |  W   |  T   | 5.00  |
|  3 |  2  |  W   |  T   | 7.00  |

I want a select statement to output with a user selected TR_QTR = 1 I expect:

| ID | Name          | Total TR_Amt                | Total TR_AMT                 |
|    |               |(TR_Type = W and TR_Xref = F)| (TR_Type = W and TR_Xref = T)|
+----+---------------+-----------------------------+------------------------------+
|  1 |sam spade      |    8.25                     |      6.67                    |
|  2 |junior samples |    18.58                    |      6.10                    |
|  3 |april showers  |    4.00                     |      8.00                    |

Do I just do an left join of the same table twice (Transaction File) for each of the amounts I want to total ?

Any help would be apprecitated

You want something like:

SELECT 
  e.Emp_ID
, e.Emp_name
, SUM(CASE WHEN t.TR_Type = 'W' AND t.TR_Xref = 'F') THEN TR_Amt ELSE 0 END) AS Total_W_F
, SUM(CASE WHEN t.TR_Type = 'W' AND t.TR_Xref = 'T') THEN TR_Amt ELSE 0 END) AS Total_W_T
FROM Employee_Table e
LEFT OUTER JOIN Transaction_Table t ON e.Emp_ID = t.TR_ID
WHERE t.TR_QTR = 1
GROUP BY
  e.Emp_ID
, e.Emp_name

Basically for each row you SUM TR_Amt where the conditions are true, otherwise SUM 0.

Group your transactions by employee id and build sums for the two XREFs:

select
  e.id,
  e.name,
  coalesce(t.sumf, 0) as total_f,
  coalesce(t.sumt, 0) as total_t
from employee e
left join
(
  select 
    id,
    sum(case when xref = 'F' then amt end) as sumf,
    sum(case when xref = 'T' then amt end) as sumt
  from tr
  where type = 'W'
  and qtr = 1
  group by id
) t on t.id = e.id;

This should do it.

WITH tr_wf AS (
SELECT tr_id as wfid, sum(tr_amt) as tr1
  FROM transaction
 WHERE tr_type = 'W' AND tr_xref = 'F'
GROUP BY tr_id
), tr_wt AS (
SELECT tr_id as wtid, sum(tr_amt) as tr2
  FROM transaction
 WHERE tr_type = 'W' AND trxref = 'T'
GROUP BY tr_id
)
SELECT e.emp_id, e.emp_name, tr1 AS total_tr_amt_w_f, tr2 AS total_tr_amt_w_t
  FROM employee e
  JOIN tr_wf ON e.emp_id = wfid
  JOIN tr_wt ON e.emp_id = wtid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM