我的表:
HEAD_ID LINE ID ADJUST_ID LIST_PRICE DISCOUNT
01 01 01 200 15
01 01 02 200 0
01 01 03 200 10
01 02 01 300 16
01 02 02 300 0
02 01 01 300 15
02 01 02 300 0
02 01 03 300 10
02 02 01 100 16
02 02 02 100 0
我需要通过HEAD_ID和LINE_ID对LIST_PRICE组求和,但不包括ADJUST_ID。 TOTAL_LIST_PRICE应该是头ID中所有行ID的总和但不重复。例如:
For the TOTAL_LIST_PRICE for HEAD_ID 01 is (HEAD_ID 01 LINE_ID 01) + (HEAD_ID 01 LINE_ID 02) = 200 + 300 = 500
For the TOTAL_LIST_PRICE for HEAD_ID 02 is (HEAD_ID 02 LINE_ID 01) + (HEAD_ID 02 LINE_ID 02) = 300 + 105 = 405
预期结果如下:
HEAD_ID LINE ID ADJUST_ID LIST_PRICE DISCOUNT TOTAL_LIST_PRICE
01 01 01 200 15 500
01 01 02 200 0 500
01 01 03 200 10 500
01 02 01 300 16 500
01 02 02 300 0 500
02 01 01 300 15 405
02 01 02 300 0 405
02 01 03 300 10 405
02 02 01 105 16 405
02 02 02 105 0 405
我的查询是这样的 -
SELECT head_id, line_id, adjust_id, list_price, discount,
SUM(LIST_PRICE) OVER (PARTITION BY head_id, line_id) TOTAL_LIST_PRICE
FROM TABLE;
输出 -
HEAD_ID LINE ID ADJUST_ID LIST_PRICE DISCOUNT TOTAL_LIST_PRICE
01 01 01 200 15 600
01 01 02 200 0 600
01 01 03 200 10 600
01 02 01 300 16 600
01 02 02 300 0 600
02 01 01 300 15 900
02 01 02 300 0 900
02 01 03 300 10 900
02 02 01 105 16 210
02 02 02 105 0 210
SUM OVER PARTITION中是否有任何遗漏?或者我使用了错误的方法?
由于
答案 0 :(得分:1)
select head_id,
line_id,
adjust_id,
list_price,
discount,
sum(case when fl = 1 then list_price end) over(partition by head_id) total_list_price
from (select head_id, line_id, adjust_id, list_price, discount,
row_number()over(partition by head_id, line_id order by head_id) fl
from tab) q;
答案 1 :(得分:1)
您可以使用ROW_NUMBER
分析函数和PARTITION BY Head_id, Line_id, List_Price
对该分区的每一行进行编号,然后在ROW_NUMBER
为1
的情况下进行过滤将给出唯一的List_Price
1}}对于每个Head_id, Line_id
,每个SUM
只需要Head_id
med,以提供您想要的输出:
Oracle 11g R2架构设置:
CREATE TABLE table_name ( HEAD_ID, LINE_ID, ADJUST_ID, LIST_PRICE, DISCOUNT ) AS
SELECT '01', '01', '01', 200, 15 FROM DUAL UNION ALL
SELECT '01', '01', '02', 200, 0 FROM DUAL UNION ALL
SELECT '01', '01', '03', 200, 10 FROM DUAL UNION ALL
SELECT '01', '02', '01', 300, 16 FROM DUAL UNION ALL
SELECT '01', '02', '02', 300, 0 FROM DUAL UNION ALL
SELECT '02', '01', '01', 300, 15 FROM DUAL UNION ALL
SELECT '02', '01', '02', 300, 0 FROM DUAL UNION ALL
SELECT '02', '01', '03', 300, 10 FROM DUAL UNION ALL
SELECT '02', '02', '01', 100, 16 FROM DUAL UNION ALL
SELECT '02', '02', '02', 100, 0 FROM DUAL;
查询1 :
SELECT HEAD_ID,
LINE_ID,
ADJUST_ID,
LIST_PRICE,
DISCOUNT,
SUM( CASE rn WHEN 1 THEN LIST_PRICE END ) OVER ( PARTITION BY Head_id )
AS total_list_price
FROM (
SELECT t.*,
ROW_NUMBER() OVER ( PARTITION BY Head_id, Line_id, List_Price
ORDER BY ROWNUM ) AS rn
FROM table_name t
)
<强> Results 强>:
| HEAD_ID | LINE_ID | ADJUST_ID | LIST_PRICE | DISCOUNT | TOTAL_LIST_PRICE |
|---------|---------|-----------|------------|----------|------------------|
| 01 | 01 | 01 | 200 | 15 | 500 |
| 01 | 01 | 02 | 200 | 0 | 500 |
| 01 | 01 | 03 | 200 | 10 | 500 |
| 01 | 02 | 01 | 300 | 16 | 500 |
| 01 | 02 | 02 | 300 | 0 | 500 |
| 02 | 01 | 01 | 300 | 15 | 400 |
| 02 | 01 | 02 | 300 | 0 | 400 |
| 02 | 01 | 03 | 300 | 10 | 400 |
| 02 | 02 | 01 | 100 | 16 | 400 |
| 02 | 02 | 02 | 100 | 0 | 400 |