按组计数作为SQL Server查询的一部分

时间:2015-03-12 09:03:49

标签: sql-server group-by

我有以下SQL服务器查询:

SELECT G.StyleID, 
G.Size, Count(*) As 'Count', 
G.PropertyID FROM TBL_Garment G 
LEFT OUTER JOIN TBL_EmployeeJob ON TBL_EmployeeJob.ID = G.EmployeeJobID 
LEFT OUTER JOIN TBL_DepartmentJob ON TBL_DepartmentJob.ID = TBL_EmployeeJob.DepartmentJobID 
LEFT OUTER JOIN TBL_Department ON TBL_Department.ID = TBL_DepartmentJob.DepartmentID 
LEFT OUTER JOIN TBL_Division ON TBL_Division.ID = TBL_Department.DivisionID 
LEFT OUTER JOIN TBL_Job ON TBL_Job.ID = TBL_DepartmentJob.JobID 
WHERE G.EmployeejobiD IS NOT NULL  
AND TBL_Division.ID = N'1'  
AND TBL_Department.ID = N'1'  
AND TBL_Job.ID = N'1'  
AND G.PropertyID = 1 
GROUP BY G.StyleID, G.Size, G.PropertyID 
ORDER BY G.StyleID

以下是此查询返回的结果:

enter image description here

现在我需要在此表中添加2个列:

一个是StyleID(总计)的计数总和,另一个是计数/总计。

我确信我可以自己获取计数/总数,但不知道如何获得总计列,或者即使可能。>

以下是我希望表格的版本:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用窗口函数:

sum([Count]) over (partition by [StyleID])

尽管如此,请确保它对你的表现非常好。