日期:2014-05-17  浏览次数:20802 次

嵌套查询的 外面那句sql 怎么使用group by呢?


SELECT a.code, WMSYS.WM_CONCAT(a.name),
(SELECT COUNT(*) from table2 WHERE table2.id = a.id) as totalCount
from
table1 a
where a.id=82
group by a.code , ???


里面使用了WMSYS.WM_CONCAT,必须得gourp by 一下,code到可以,但“totalCount”怎么办呢,我直接写
group by a.code , totalCount
要报错[Err] ORA-00904: "STUDENTCOUNT": invalid identifier
gourpby后不跟totalCount 又要写[Err] ORA-00979: not a GROUP BY expression

------解决方案--------------------
改成如下这样就OK了:


select code, wcname, (SELECT COUNT(1) from table2 WHERE table2.id = x.id) totalCount
from (SELECT a.id id, a.code code , WMSYS.WM_CONCAT(a.name) wcname,
from table1 a where a.id=82 group by a.id, a.code ) x