Using distinct on a column and doing order by on another column gives an error Using distinct on a column and doing order by on another column gives an error oracle oracle

Using distinct on a column and doing order by on another column gives an error


As far as i understood from your question .

distinct :- means select a distinct(all selected values should be unique).order By :- simply means to order the selected rows as per your requirement .

The problem in your first query is For example : I have a table

ID name01 a02 b03 c04 d 04 a

now the query select distinct(ID) from table order by (name) is confused which record it should take for ID - 04 (since two values are there,d and a in Name column). So the problem for the DB engine is here when you say order by (name).........


You might think about using group by instead:

select n_numfrom abc_testgroup by n_numorder by min(k_str)


The first query is impossible. Lets explain this by example. we have this test:

n_num k_str2     a2     c1     b

select distinct (n_num) from abc_test is

21

Select n_num from abc_test order by k_str is

212

What do you want to return

select distinct (n_num) from abc_test order by k_str?

it should return only 1 and 2, but how to order them?