以下是选择具有最大行数的配置类型的查询。 还有另一种方法与第二次查询一样快,以选择数据吗?
select CONFIG_TYPE, MAX(COUNTING) FROM
(select CONFIG_TYPE, COUNT(*) as COUNTING FROM NOTIFICATION_CONFIG GROUP BY CONFIG_TYPE)
WHERE COUNTING =
(select MAX(COUNTING) FROM
(select COUNT(*) as COUNTING FROM NOTIFICATION_CONFIG GROUP BY CONFIG_TYPE)
)GROUP BY CONFIG_TYPE
第二个查询将在1次搜索中完成工作,而不像在2中执行该操作。
select CONFIG_TYPE, COUNTING FROM
(select CONFIG_TYPE, COUNT(*) as COUNTING FROM NOTIFICATION_CONFIG
GROUP BY CONFIG_TYPE ORDER BY COUNTING DESC FETCH FIRST 1 ROW ONLY)
只是想知道是否有另一种方法可以做到这一点。
答案 0 :(得分:1)
使用窗口功能!
SELECT c.*
FROM (SELECT CONFIG_TYPE, COUNT(*) as COUNTING,
RANK() OVER (ORDER BY COUNT(*) DESC) as seqnum
FROM NOTIFICATION_CONFIG
GROUP BY CONFIG_TYPE
) c
WHERE seqnum = 1;
这使用RANK()
,因此它将返回所有最大值(在tie的情况下)。如果你想在关系的情况下返回一个任意最大值,那么请改用ROW_NUMBER()
。或者,如果你不想要关系,你可以这样做:
SELECT CONFIG_TYPE, COUNT(*) as COUNTING
FROM NOTIFICATION_CONFIG
GROUP BY CONFIG_TYPE
ORDER BY COUNT(*) DESC
FETCH FIRST 1 ROW ONLY;
答案 1 :(得分:0)
这只是搞砸了
select CONFIG_TYPE, MAX(COUNTING)
FROM ( select CONFIG_TYPE, COUNT(*) as COUNTING
FROM NOTIFICATION_CONFIG
GROUP BY CONFIG_TYPE
)
WHERE COUNTING = ( select MAX(COUNTING)
FROM ( select COUNT(*) as COUNTING
FROM NOTIFICATION_CONFIG
GROUP BY CONFIG_TYPE
)
)
GROUP BY CONFIG_TYPE