如何在SQL Server中使用group by

时间:2014-11-30 10:28:57

标签: sql sql-server

查询:

SELECT 
   dbo.tblDivision.DivisionName, dbo.tblDistrict.DistrictName,
   case 
      when Gender = 'Male' 
         then count(Gender)
   end as male,
   case 
      when Gender = 'female'
        then count(Gender)
   end as female,
   UnitEName
FROM 
   dbo.tblDistrict 
INNER JOIN
   dbo.tblThana ON dbo.tblDistrict.DistrictNo = dbo.tblThana.DistrictNo 
INNER JOIN
   dbo.tblDivision ON dbo.tblDistrict.DivisionNo = dbo.tblDivision.DivisionNo 
INNER JOIN
   dbo.vw_EmpInfo ON dbo.tblThana.ThanaNo = dbo.vw_EmpInfo.PerThanaNo 
GROUP BY
   Gender, DistrictName, DivisionName, UnitEName, UnitEAddress 
ORDER BY 
   DivisionName, DistrictName, UnitEName

结果如下:

enter image description here

但我希望每个单位的结果都在一行中。可能是我的小组中有问题。

我应该如何重构我的查询?

4 个答案:

答案 0 :(得分:0)

您必须将Gender列从分组子句中删除:

SELECT dbo.tblDivision.DivisionName, dbo.tblDistrict.DistrictName,
       COUNT(CASE Gender WHEN 'Male' THEN 1 END) AS male,
       COUNT(CASE Gender WHEN 'female' THEN 1 END) AS female,
       UnitEName
FROM dbo.tblDistrict 
INNER JOIN dbo.tblThana ON dbo.tblDistrict.DistrictNo = dbo.tblThana.DistrictNo 
INNER JOIN dbo.tblDivision ON dbo.tblDistrict.DivisionNo = dbo.tblDivision.DivisionNo 
INNER JOIN dbo.vw_EmpInfo ON dbo.tblThana.ThanaNo = dbo.vw_EmpInfo.PerThanaNo 
GROUP BY DistrictName,DivisionName,UnitEName,UnitEAddress 
ORDER BY DivisionName,DistrictName,UnitEName

答案 1 :(得分:0)

如果您想要同时存在两种性别,则不应按性别分组。

相反,你应该使用sum()来计算有多少男性和多少女性:

SELECT dbo.tblDivision.DivisionName, dbo.tblDistrict.DistrictName,
  SUM(case when Gender='Male' then 1 else 0 end) as male,
  SUM(case when Gender='Female' then 1 else 0 end) as female,
  UnitEName
FROM dbo.tblDistrict INNER JOIN
  dbo.tblThana ON dbo.tblDistrict.DistrictNo = dbo.tblThana.DistrictNo INNER JOIN
  dbo.tblDivision ON dbo.tblDistrict.DivisionNo = dbo.tblDivision.DivisionNo INNER JOIN
  dbo.vw_EmpInfo ON dbo.tblThana.ThanaNo = dbo.vw_EmpInfo.PerThanaNo 
group by DistrictName,DivisionName,UnitEName,UnitEAddress 
order by DivisionName,DistrictName,UnitEName

答案 2 :(得分:0)

SELECT dbo.tblDivision.DivisionName
     , dbo.tblDistrict.DistrictName
     ,COUNT(case when Gender='Male' then 1 end) as male
     ,COUNT(case when Gender='female' then 1 end) as female
     ,UnitEName
FROM dbo.tblDistrict 
INNER JOIN dbo.tblThana ON dbo.tblDistrict.DistrictNo = dbo.tblThana.DistrictNo 
INNER JOIN dbo.tblDivision ON dbo.tblDistrict.DivisionNo = dbo.tblDivision.DivisionNo 
INNER JOIN dbo.vw_EmpInfo ON dbo.tblThana.ThanaNo = dbo.vw_EmpInfo.PerThanaNo 
GROUP BY DivisionName
        ,DistrictName
        ,UnitEName

答案 3 :(得分:0)

Gender子句

中删除Group by
SELECT dbo.tblDivision.DivisionName, dbo.tblDistrict.DistrictName,
sum (case when Gender='Male' then 1 else 0 end) as male
sum (case when Gender='female' then 1 else 0 end)  as female,
       UnitEName
FROM dbo.tblDistrict 
INNER JOIN dbo.tblThana ON dbo.tblDistrict.DistrictNo = dbo.tblThana.DistrictNo 
INNER JOIN dbo.tblDivision ON dbo.tblDistrict.DivisionNo = dbo.tblDivision.DivisionNo 
INNER JOIN dbo.vw_EmpInfo ON dbo.tblThana.ThanaNo = dbo.vw_EmpInfo.PerThanaNo 
GROUP BY DistrictName,DivisionName,UnitEName,UnitEAddress 
ORDER BY DivisionName,DistrictName,UnitEName