有一張表test,字段分別是主鍵id,road_id,user_id,region,現在的需求是對road_id分組,然後將region欄位用逗號拼接起來
test表:
分組的sql如下:
select road_id , listagg(region, ',') within group (order by road_id) region
from rasmp_test where region is not null group by road_id
分組後的結果:
發現region有重複的數字,需要對其進行去重
修改後的sql如下:
select road_id , listagg(region, ',') within group (order by road_id) region from (
select distinct road_id, region from rasmp_test
) t1 where region is not null group by road_id;
執行結果:
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 顯示其他字段
原先用select key,max value from tbl group by key 查詢出的結果,但是我要再多加乙個test欄位 根據的語句結合起來,以下是可以顯示其他字段,並且考慮排名相同的情況 select t.key t.value,listagg t.test,within grou...