简体   繁体   中英

How to group an already grouped data-set in oracle sql

问题数据集

预期产出

I want to first group data as per "Roll" and then further group it as per "Name" with different "Marks" . I have grouped data using Group by and having but I am not sure how to further group it.

Group by two columns:

SELECT Roll,Name FROM table GROUP BY Roll,Name;

If you want to see marks also you have aggegrate(sum) it like:

SELECT Roll,Name,sum(Marks)as 'Total Marks' FROM table GROUP BY Roll,Name;

If you want to see marks also you have aggegrate(average) it like:

SELECT Roll,Name,avg(Marks)as 'Average Marks' FROM table GROUP BY Roll,Name;

Refer this image

If I understand correctly, you want roll / name pairs that have multiple distinct values. You can do this using window functions:

select distinct roll, name, marks
from (select t.*, count(distinct marks) over (partition by roll, name) as cnt
      from t
     ) t
where cnt > 1;

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