-2

是否可以使用提供 SQL 查询的帮助来提取最大数量“columnb”值为“Active”的“columna”值。意味着在 columnb 中有一个值 "Active" ,我想拉出具有最大值 Active n columnb 的 columna 值。

我正在寻找输出为 columna = M1 和 Count = 4

columna     columnb
M1          Active
M1          Active
M1          Active
M1          Active
M2          failed
M2          failed
M2          failed
M3          pending
M3          pending
M3          pending
4

4 回答 4

1
SELECT top 1 columna,COUNT(*) as cnt
FROM Table1
WHERE columnb = 'Active'
GROUP BY columna
order by cnt desc

小提琴

于 2013-08-28T12:17:56.333 回答
1

您请求的结果将通过以下方式产生:

SELECT columna,COUNT(*)
FROM Table
WHERE columnb = 'Active'
GROUP BY columna
于 2013-08-28T12:13:49.333 回答
0

RDMBS-es 之间的语法略有不同,但逻辑仍然存在。过滤您的行,按它们columnb分组columna,按顺序排列count(*)并选择前 1

SQL 服务器:

SELECT TOP 1 columna, COUNT(*) AS Count
FROM YourTable
WHERE columnb = 'Active'
GROUP BY columna
ORDER BY COUNT(*) DESC

SQLFiddle 演示

MySQL:

SELECT columna, COUNT(*) AS Count
FROM YourTable
WHERE columnb = 'Active'
GROUP BY columna
ORDER BY COUNT(*) DESC
LIMIT 1

SQLFiddle 演示

于 2013-08-28T12:20:22.517 回答
0
SELECT columna,count(*) FROM TABLE_NAME where columnb = "Active" GROUP BY columna
于 2013-08-28T12:17:56.647 回答