You want to select distinct values from a column in a table. No problem.
You use DISTINCT or GROUP BY in your query. But did you know that there
is a third way to suppress duplicates? For example:
Both
andselect distinct department_id from employees
returnselect department_id from employees group by department_id
Boring. Nothing new here.DEPARTMENT_ID ---------------------- 100 30 90 20 70 110 50 80 40 60 10 12 rows selected
But wait a minute, here is what I learnt today, another way to return distinct department ids, using the function ROW_NUMBER:
returnsselect department_id from ( select department_id, row_number( ) over (partition by department_id order by department_id) rownumber from employees ) t where rownumber = 1
It’s the same output as the first two queries, except that it is ordered differently.DEPARTMENT_ID ---------------------- 10 20 30 40 50 60 70 80 90 100 110 12 rows selected
To be honest, I will continue using DISTINCT or GROUP BY for such
requirements, however, it does not hurt to know that there are
alternatives.
Tidak ada komentar:
Posting Komentar