oracle group by 顯示其他字段

2022-02-11 05:18:12 字數 718 閱讀 9457

原先用select key,max(value) from tbl group by key

查詢出的結果,但是我要再多加乙個test欄位

根據網友的語句結合起來,以下是可以顯示其他字段,並且考慮排名相同的情況:

select

t.key

, t.value,

listagg(t.test, ',

') within group( order

by t.key, t.value) --

同名排名用','分隔

from

(select rank() --

跳序排序1、1、3...

over(partition by tb1.key

--開窗聚合函式

order

by tb1.value desc ) as

rownum1,

tb1.

key, tb1.value, tb1.test,

from

tb1order

by tb1.key

asc) t

where t.rownum1 =

1group

by t.key, t.value;

oracle group by的擴充套件

1 rollup select job,sum sal from emp group by rollup job 將增加一行總的合計數。2 cube select job,deptno,sum sal from emp group by cube job,deptno 先按job和deptno的各種...

oracle group by 的各種用法

1 group by group by 是分組函式,是把by後面的資料進行分組,如果後面是乙個字段,就把乙個字段當成一組,如果是兩個欄位就把兩個字段當成一組,以此類推。例如 select job,ename,sum sal from emp group by job,ename order by e...

oracle group by字串拼接

有一張表test,字段分別是主鍵id,road id,user id,region,現在的需求是對road id分組,然後將region欄位用逗號拼接起來 test表 分組的sql如下 select road id listagg region,within group order by road ...