在group by中对具有最大值的行进行计数

时间:2019-08-07 12:25:53

标签: sql oracle

我有一张这样的桌子:

| ID  | CATEGORY | PRIORITY |
| --- | -------- | -------- |
| 1   | dogs     | 1        |
| 2   | dogs     | 2        |
| 3   | cats     | 1        |
| 4   | cats     | 2        |
| 5   | cats     | 2        |

我想要得到的是如下所示的分组结果集:

| CATEGORY | HIGHEST_PRIORITY | NMB_ROWS_WITH_HIGHEST_PRIO |
| -------- | ---------------- | -------------------------- |
| dogs     | 2                | 1                          |
| cats     | 2                | 2                          | 

我想象以下形式的查询可以工作:

SELECT CATEGORY,
  MAX(PRIORITY),  -- Highest Priority of Category
  COUNT(MAX(PRIORITY)) -- Count how many rows within a category have the highest priority
FROM MY_TABLE
GROUP BY CATEGORY;

,但是用这种方法无法计算组中COUNT(MAX(PRIORITY))的行数。有没有不用子查询就能实现的方法吗?
(我使用Oracle 12c数据库)

8 个答案:

答案 0 :(得分:3)

窗口函数在这里很方便。我们可以先对该表进行一次迭代,然后为每个类别生成最高优先级值。然后,汇总并有条件计数每个优先级等于最大优先级的次数。

WITH cte AS (
    SELECT t.*,
        MAX(PRIORITY) OVER (PARTITION BY CATEGORY) MAX_PRIORITY
    FROM MY_TABLE t
)

SELECT
    CATEGORY,
    MAX(PRIORITY) AS HIGHEST_PRIORITY,
    COUNT(CASE WHEN PRIORITY = MAX_PRIORITY THEN 1 END) AS NMB_ROWS_WITH_HIGHEST_PRIO
FROM CTE
GROUP BY
    CATEGORY;

enter image description here

Demo

答案 1 :(得分:1)

您可以简单地使用Clause来实现它 简单样本

SELECT CATEGORY, MAX(PRIORITY) AS HIGHEST_PRIORITY, COUNT(MAX(PRIORITY)) AS NMB_ROWS_WITH_HIGHEST_PRIO 
FROM MY_TABLE 
GROUP BY CATEGORY 
HAVING MAX(PRIORITY) >1;
  

希望这对您有帮助

答案 2 :(得分:0)

您可以在下面尝试-

select A.category,HIGHEST_PRIORITY,NMB_ROWS_WITH_HIGHEST_PRIO 
from
(
SELECT CATEGORY,MAX(PRIORITY) as HIGHEST_PRIORITY FROM MY_TABLE
GROUP BY CATEGORY
)A inner join 
(SELECT CATEGORY,PRIORITY,count(*) as NMB_ROWS_WITH_HIGHEST_PRIO FROM MY_TABLE
GROUP BY CATEGORY,PRIORITY
)B on A.CATEGORY=B.CATEGORY and A.HIGHEST_PRIORITY=B.PRIORITY

答案 3 :(得分:0)

我认为可以使用窗口功能来实现-

SELECT CATEGORY, PRIORITY, COUNT(PRIORITY)
FROM (SELECT CATEGORY
            ,PRIORITY
            ,DENSE_RANK() OVER (PARTITION BY CATEGORY ORDER BY PRIORITY DESC) RN
      FROM MY_TABLE) X
WHERE RN = 1
GROUP BY CATEGORY, PRIORITY

这里是Fiddle

答案 4 :(得分:0)

您可以将ROW_NUMBER()用于聚合:

SELECT CATEGORY, PRIORITY, CNT
FROM (SELECT CATEGORY, PRIORITY, COUNT(*) as CNT,
             ROW_NUMBER() OVER (PARTITION BY CATEGORY ORDER BY COUNT(*) DESC) as seqnum
      FROM MY_TABLE
      GROUP BY CATEGORY, PRIORITY
     ) cp
WHERE seqnum = 1;

答案 5 :(得分:0)

select mt.category, max(mt.priority), count(mt.priority)
from my_table mt
where priority = (select max(mt2.priority)
                  from my_table mt2
                  where mt1.category = mt2.category)
group by mt.category;

答案 6 :(得分:0)

SELECT `PRIORITY`, count(`PRIORITY`) as count
FROM tablename
GROUP BY PRIORITY
ORDER BY count desc

答案 7 :(得分:0)

您可以使用self-joinaggregate functions来做到这一点。

SELECT t2.category        AS CATEGORY,
       Max(t1.priority)   AS HIGHEST_PRIORITY,
       Count(t2.priority) AS NMB_ROWS_WITH_HIGHEST_PRIO
FROM   (SELECT Max(priority) AS priority, category
        FROM   my_table
        GROUP  BY category) t1
       inner join my_table t2 ON t1.category = t2.category AND t1.priority = t2.priority
GROUP  BY t2.category