我需要查询才能获得每个级别的参与者数量。
注意:4级参与者不应该处于其他级别(3,2,1级)。即我只对每个参与者的最高级别感兴趣。
表:
+----+-------+----------+
| ID | Level | Date |
+----+-------+----------+
| 38 | 1 | 06 -05 |
| 38 | 2 | 08 -05 |
| 38 | 3 | 12 -05 |
| 38 | 4 | 13 -05 |
| 39 | 1 | 13 -05 |
| 39 | 2 | 13 -05 |
| 40 | 1 | 12 -05 |
+----+-------+----------+
需要输出:
+-------+-------+
| Count | Level |
+-------+-------+
| 1 | 1 |
| 1 | 2 |
| 0 | 3 |
| 1 | 4 |
+-------+-------+
答案 0 :(得分:2)
试试这个:
SELECT count(t2.Id) as count,t1.Level as level
FROM table1 t1
LEFT JOIN
(SELECT Id,MAX(Level) as Level
FROM table1
GROUP BY Id ) t2 on t1.Id=t2.Id and t1.Level=t2.Level
GROUP BY t1.Level
您可以在SQL Fiddle
上进行测试答案 1 :(得分:0)
我认为你正在寻找
Select Count(*), Level FROM table_name GROUP BY Level
答案 2 :(得分:0)
预期结果是知道有多少人将级别作为最高级别,按级别进行分组。
SELECT count(id) as count, level
FROM
(
SELECT id, max(level) as level
FROM myTable
GROUP BY id
) t1
GROUP BY level;