SELECT DISTINCT on multiple columns along with other columns [duplicate]

隐身守侯 提交于 2019-12-11 23:21:04

问题


I want to run query like below

select distinct (columnA, columnB, columnC), columnD from MY_TABLE where columnA IS NOT NULL AND columnB IS NOT NULL AND columnC is NOT NULL;

I only want distinct of columnA, columnB and columnC and NOT ON columnD. But SQL developer is pointing an error right after columnA, How can I fix this?

I tried to fix my query using GROUP BY

select columnA, columnB, columnC, (select count(*) from TABLE2 WHERE table2columnA = myTable.columnA) from MY_TABLE myTable where columnA IS NOT NULL AND columnB IS NOT NULL AND columnC is NOT NULL GROUP BY columnA, columnB, columnC;

Notice that my columnD is actually another select statement? But this is giving me error

ORA-00979: not a GROUP BY expression
00979. 00000 -  "not a GROUP BY expression"

This is not a duplicate of that another question


回答1:


SELECT DISTINCT a,b,c FROM t

is roughly equivalent to:

SELECT a,b,c FROM t GROUP BY a,b,c,t

It's a good idea to get used to the GROUP BY syntax, as it's more powerful. It's a good idea to get used to the GROUP BY syntax, Please see this post:

Possible duplicate ?



来源:https://stackoverflow.com/questions/30106745/select-distinct-on-multiple-columns-along-with-other-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!