2015-07-12 61 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;