2015-07-12 62 views
-4

我有一個里程碑表中的字段的顯示數: enter image description hereSQL - 從另一個表

和狀態表:

enter image description here

我有一個查詢:

SELECT T1.Status,T2.MilestoneTitle FROM [Organisation].[dbo].[Status] T1 
JOIN [Organisation].[dbo].[Milestone] T2 
ON T1.StatusId=T2.MilestoneStatusId WHERE T2.ProjectId=4 

其輸出爲:

enter image description here

現在,我想顯示輸出:

enter image description here

如何查詢爲該寫?

+0

爲什麼downvotes? – Engineer

回答

0

通過使用計數和組就可以達到這個

SELECT 
    T1.Status, COUNT(T2.MilestoneTitle) AS MilestoneTitleCount 
FROM 
    [Organisation].[dbo].[Status] T1 
JOIN 
    [Organisation].[dbo].[Milestone] T2 ON T1.StatusId = T2.MilestoneStatusId 
WHERE 
    T2.ProjectId = 4 
GROUP BY 
    T1.Status 
ORDER BY 
    T1.Status DESC 
0
SELECT Status, COUNT(MilestoneTitle) AS MilestoneTitleCount 
FROM Organisation.dbo.Status INNER JOIN 
Organisation.dbo.Milestone ON StatusId = MilestoneStatusId 
WHERE (ProjectId = 4) 
GROUP BY Status 

COUNT()函數返回符合指定條件的的行數。

SELECT COUNT(column_name) FROM table_name; 

GROUP BY語句一起使用與集合函數到組由一個或多個列中的結果集。

SELECT column_name, Count(column_name) 
FROM table_name 
WHERE <Confidtion> 
GROUP BY column_name;