2013-08-28 88 views
-2

是否有人可以幫助提供SQL查詢,以便將具有最大數目「columnb」值的「columna」值拉出爲「Active」。在columnb中意味着有一個值「活動」,我想拉動最大值爲Active n columnb的columna值。SQL查詢所需的建議

我期待輸出爲columna = M1和計數= 4

columna  columnb 
M1   Active 
M1   Active 
M1   Active 
M1   Active 
M2   failed 
M2   failed 
M2   failed 
M3   pending 
M3   pending 
M3   pending 
+3

問題不清楚,請提供您已經嘗試過的內容,示例數據和預期結果。 – ganders

+0

columnB的一些典型值是什麼? –

+2

您使用的是什麼RDBMS?獲取最大行的語法可能不同。 –

回答

1

您請求將通過以下產生的結果:

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

FIDDLE

+0

讓我試試這個並回來。 – user2725264

0
SELECT columna,count(*) FROM TABLE_NAME where columnb = "Active" GROUP BY columna 
0

RDMBS-es之間的語法略有不同,但邏輯依然存在。通過columna過濾您行基於columnb,它們分組,責令其在count(*)選擇頂部1

SQL服務器:

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

SQLFiddle DEMO

的MySQL:

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

SQLFiddle DEMO