2016-12-05 101 views
-1

假設我有3列是這樣的:SQL分組列

Status, QName, Item 
Broken, q1, i1 
Broken, q1, i2 
Broken, q1, i3 
Broken, q2, i4 
Broken, q3, i5 
Immediate,q1,i6 
Immediate,q2,i7 

我想這樣的輸出:

Status = Broken, No of queues = 3, total items = 5 
Status = Immediate, No of queues = 2, total items = 2 

我怎樣才能在一個查詢此輸出?

+1

請分享你有什麼到目前爲止已經試過。 – jera

回答

1

您可以使用COUNT(DISTINCT)此:

Select Status, 
      Count(Distinct QName) As NoQueues, 
      Count(Distinct Item) As TotalItems 
From  YourTable 
Group By Status 
1

試試這個:

SELECT Status, 
     COUNT(DISTINCT QName) [No of queues], 
     COUNT(DISTINCT Item) [total items] 
FROM [YourTable] 
GROUP BY Status