Select distinct and non-distinct column values Select distinct and non-distinct column values oracle oracle

Select distinct and non-distinct column values


Do you want list_agg?

select colA, colB,       list_agg(distinct colC, ','),       list_agg(distinct colD, ',')from Table1Group by ColA, ColB

If any arbitrary value would do for ColC and colD, you can use min():

select colA, colB, min(colC), min(colD)from Table1Group by ColA, ColB


The DISTINCT applies to all values you are selecting, not just to some columns. In your case it will apply to all: colA, colB, colC, colD. It is impossible to select all columns and make some distinct and some not. The only way to do this shown in Gordon's example, this is the only valid example and answer.


Maybe use plain set operations

with UNION and UNION ALL (see http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries004.htm)

UNION colA and colB will eliminate doubled entries in these columns whereas UNION ALL will keep all values from the colC and colD