这是我的问题:
SELECT GENRE,MAX(COUNT(GENRE))
FROM BOOKS
WHERE ID_BOOK IN(SELECT ID_BOOK
FROM SIGNATURES
WHERE SIGNATURE IN(SELECT SIGNATURE
FROM ORDERS
WHERE ID_READER=1))
GROUP BY GENRE;
我收到错误:
SQL错误:ORA-00937:不是单组组功能
当我删除保留在SELECT之后的GENRE时它可以正常工作但只生成数字。我还需要知道哪个特定的GENRE具有最大值。
编辑:
select GENRE,CNT INTO var_genre, var_max from (
SELECT GENRE,COUNT(GENRE) as CNT FROM BOOKS
WHERE ID_BOOK IN(SELECT ID_BOOK FROM SIGNATURES
WHERE SIGNATURE IN(SELECT SIGNATURE FROM ORDERS WHERE ID_READER=1))
GROUP BY GENRE
order by COUNT(GENRE) desc)
where rownum < 2;
答案 0 :(得分:0)
您无法在一个查询中进行聚合。你应该在子查询中做到这一点:
select distinct GENRE,CNT from (
select GENRE,CNT, DENSE_RANK() Over(prder by CNT) as rnk from (
SELECT GENRE,
COUNT(GENRE) over (partition by GENRE) as CNT
FROM BOOKS
WHERE ID_BOOK IN(SELECT ID_BOOK
FROM SIGNATURES
WHERE SIGNATURE IN(SELECT SIGNATURE FROM ORDERS WHERE ID_READER=1))
)
whre rnk = 1;
或者,如果您只需要一行,则可以将其包装到子查询中:
select GENRE,CNT from (
SELECT GENRE,COUNT(GENRE) as CNT FROM BOOKS
WHERE ID_BOOK IN(SELECT ID_BOOK FROM SIGNATURES
WHERE SIGNATURE IN(SELECT SIGNATURE FROM ORDERS WHERE ID_READER=1))
GROUP BY GENRE
order by COUNT(GENRE) desc)
whre rownum < 2;