选择总和声明

时间:2010-10-29 19:37:38

标签: sql

我创建了这个select语句,我将转换为视图。我需要帮助。我需要能够在报告页面上添加= Yes和No total的总数。

select
    ps.BidPackage_ID,
    ps.Project_ID,
    SUM (case ps.Minority when 'Yes' then 1 else 0 end) MinorityTotal,
    SUM (case ps.Gender when 'Female' then 1 else 0 end) FemaleTotal,
    SUM(case ps.Cleveland_Resident when 1 then 1 else 0 end) ClevelandResidents,
    ps.SubContractor
from
    PersonnelSummary ps
group by
    ps.BidPackage_ID,
    ps.Project_ID,
    ps.SubContractor

2 个答案:

答案 0 :(得分:1)

你几乎拥有它:

...
SUM (case ps.Minority when 'Yes' then 1 else 0 end) AS MinorityYes,
SUM (case ps.Minority when 'No' then 1 else 0 end) AS MinorityNo,
COUNT(*) AS Total,
...

使用Total我假设每一行都要计算在内。这就是你想要的:

  • 列中唯一存在的值为“是”和“否”
  • 与“是”和“否”不同的值也应计入总数中。

答案 1 :(得分:0)

你逼迫我们猜你想要什么。你有一些人说他们是少数人;你想要一个说不的人数吗?或者你想要一个数字的数字,他们说“是”或“不是”,并且不包括那些“拒绝说”或根本就没有答案的人?

select
    ps.BidPackage_ID,
    ps.Project_ID,
    SUM (case ps.Minority when 'Yes' then 1 else 0 end) MinorityTotalYes,
    SUM (case ps.Minority when 'No'  then 1 else 0 end) MinorityTotalNo,
    SUM (case ps.Minority when 'Yes' then 1 when 'No' then 1 else 0 end) 
    AS StatedMinorityTotal,
    SUM (case ps.Gender when 'Female' then 1 else 0 end) FemaleTotal,
    SUM(case ps.Cleveland_Resident when 1 then 1 else 0 end) ClevelandResidents,
    ps.SubContractor
from
    PersonnelSummary ps
group by
    ps.BidPackage_ID,
    ps.Project_ID,
    ps.SubContractor