我有以下示例表,其中我需要具有列值的总和,其中VA1和Val2相同,需要求和cvalue并选择cValue最大的ID
+---------------------------+ ¦ ID ¦ Val1 ¦ Val2 ¦ CValue ¦ ¦----+------+------+--------¦ ¦ 1 ¦ 1 ¦ 1 ¦ 1 ¦ ¦----+------+------+--------¦ ¦ 2 ¦ 1 ¦ 1 ¦ 9 ¦ ¦----+------+------+--------¦ ¦ 3 ¦ 1 ¦ 1 ¦ 1 ¦ ¦----+------+------+--------¦ ¦ 4 ¦ 5 ¦ 3 ¦ 2 ¦ ¦----+------+------+--------¦ ¦ 5 ¦ 5 ¦ 3 ¦ 8 ¦ ¦----+------+------+--------¦ ¦ 6 ¦ 7 ¦ 5 ¦ 8 ¦ ¦----+------+------+--------¦ ¦ 7 ¦ 8 ¦ 9 ¦ 4 ¦ +---------------------------+
以下原始数据是必需的输出:
+---------------------------+ ¦ ID ¦ Val1 ¦ Val2 ¦ CValue ¦ ¦----+------+------+--------¦ ¦ 2 ¦ 1 ¦ 1 ¦ 11 ¦ ¦----+------+------+--------¦ ¦ 4 ¦ 5 ¦ 3 ¦ 10 ¦ ¦----+------+------+--------¦ ¦ 6 ¦ 7 ¦ 8 ¦ 8 ¦ ¦----+------+------+--------¦ ¦ 7 ¦ 8 ¦ 9 ¦ 4 ¦ +---------------------------+
有人可以帮助提供正确的查询来实现这一目标。
谢谢
答案 0 :(得分:4)
使用KEEP..DENSE_RANK
Oracle 11g R2架构设置:
CREATE TABLE yourtable
(ID int, Val1 int, Val2 int, CValue int)
;
INSERT ALL
INTO yourtable (ID, Val1, Val2, CValue)
VALUES (1, 1, 1, 1)
INTO yourtable (ID, Val1, Val2, CValue)
VALUES (2, 1, 1, 9)
INTO yourtable (ID, Val1, Val2, CValue)
VALUES (3, 1, 1, 1)
INTO yourtable (ID, Val1, Val2, CValue)
VALUES (4, 5, 3, 2)
INTO yourtable (ID, Val1, Val2, CValue)
VALUES (5, 5, 3, 8)
INTO yourtable (ID, Val1, Val2, CValue)
VALUES (6, 7, 5, 8)
INTO yourtable (ID, Val1, Val2, CValue)
VALUES (7, 8, 9, 4)
SELECT * FROM dual
;
查询1 :
SELECT MAX(ID) KEEP (
DENSE_RANK FIRST ORDER BY cvalue DESC
) as ID
,Val1
,Val2
,SUM(CValue) AS CValue
FROM yourtable
GROUP BY Val1
,Val2
<强> Results 强>:
| ID | VAL1 | VAL2 | CVALUE |
|----|------|------|--------|
| 2 | 1 | 1 | 11 |
| 5 | 5 | 3 | 10 |
| 6 | 7 | 5 | 8 |
| 7 | 8 | 9 | 4 |
答案 1 :(得分:1)
SELECT t2.id,t1.Val1,t1.Val2, SUM(t1. CValue ) AS sum
FROM table1 t1
INNER JOIN
(SELECT id,Val1,Val2 FROM Table1
WHERE cvalue IN (SELECT MAX(cvalue) FROM table1 GROUP BY Val1,Val2)) t2
ON t1.Val1 =t2.Val1 AND t1.Val2 =t2.Val2
GROUP BY t1.Val1,t1.Val2,t2.id
ORDER BY sum DESC
输出
ID VAL1 VAL2 SUM
2 1 1 11
5 5 3 10
6 7 5 8
7 8 9 4
答案 2 :(得分:0)
SELECT Val1, Val2, SUM(CValue) AS sum
FROM table
GROUP BY Val1, Val2
ORDER BY sum DESC
如果没有某种聚合策略,就无法选择ID(没有可以隐式分组的方法)。
答案 3 :(得分:0)
我建议这样的事情:
SQL> with test (id, val1, val2, cvalue) as
2 (select 1, 1, 1, 1 from dual union
3 select 2, 1, 1, 9 from dual union
4 select 3, 1, 1, 1 from dual union
5 select 4, 5, 3, 2 from dual union
6 select 5, 5, 3, 8 from dual union
7 select 6, 7, 5, 8 from dual union
8 select 7, 8, 9, 4 from dual
9 ),
10 inter as
11 (select id, val1, val2,
12 sum(cvalue) over (partition by val1, val2) cvalue,
13 row_number() over (partition by val1, val2 order by cvalue desc) rn
14 From test t
15 )
16 select id, val1, val2, cvalue
17 from inter
18 where rn = 1
19 order by id;
ID VAL1 VAL2 CVALUE
---------- ---------- ---------- ----------
2 1 1 11
5 5 3 10
6 7 5 8
7 8 9 4
SQL>
但请注意,ID的不同之处在于:ID = 4,而ID = 5。为什么?因为你说你想“选择cValue最大的ID”。对于VAL1 = 5和VAL2 = 3(ID为4和5的行),max cValue为8,它属于ID = 5.
此外,ID = 6的行应为6-7-5-8,而不是6-7-8-8。
答案 4 :(得分:0)
如果你从你的表中选择不同的值,然后将与它们相关联的cvalue求和,那应该这样做吗?
select distinct [val1], [val2] ,sum([cvalue]) from [table] group by [val1], [val2]