分组显示不显示所有行

时间:2013-03-12 11:37:12

标签: sql sql-server sql-server-2008 pivot

我有一张表tblPersonaldatatblStudentsadmitted

tblPersonalData

UID  Name Gender
------------------------ 
 E1   xyz   M
 E2   pqr   M
 E3   mno   M

tblStudentsadmitted

UID   Status  Stage
----------------------
E1     Y        1
E2     Y        2
E3     Y        1

现在我想要这样的数据:

Gender  Stage1   Stage2
 M        2       1

但在这种情况下,我没有得到女性的数据。我想要女性性别的数据,即使它是空的

我试过这个:

select 
    case 
        when gender='M' then 'Male' 
        when gender='F' then 'Female' 
    end as Gender,
    sum(case when Stage=1 then 1 else 0) end as Stage1,
    sum(case when Stage=2 then 1 else 0) end as Stage2
from  tblPersonaldata A inner join 
      tblStudentsadmitted B on A.UID=B.UID 
where B.Status='Y'
group by Gender

4 个答案:

答案 0 :(得分:3)

<击>

<击>
SELECT  CASE WHEN a.Gender = 'M' THEN 'Male' ELSE 'FEMALE' END Gender,
        SUM(CASE WHEN Stage = 1 THEN 1 ELSE 0 END) Stage1,
        SUM(CASE WHEN Stage = 2 THEN 1 ELSE 0 END) Stage2
FROM    personal a
        LEFT JOIN studentadmitted b
            ON a.UID = b.UID AND b.Status = 'Y'
GROUP   BY a.Gender

<击>

SELECT  CASE WHEN c.Gender = 'M' THEN 'Male' ELSE 'Female' END Gender,
        SUM(CASE WHEN Stage = 1 THEN 1 ELSE 0 END) Stage1,
        SUM(CASE WHEN Stage = 2 THEN 1 ELSE 0 END) Stage2
FROM    (SELECT 'F' Gender UNION SELECT 'M' Gender) c
        LEFT JOIN personal a
            ON a.Gender = c.Gender
        LEFT JOIN studentadmitted b
            ON a.UID = b.UID AND b.Status = 'Y'
GROUP   BY c.Gender

输出

╔════════╦════════╦════════╗
║ GENDER ║ STAGE1 ║ STAGE2 ║
╠════════╬════════╬════════╣
║ Female ║      0 ║      0 ║
║ Male   ║      2 ║      1 ║
╚════════╩════════╩════════╝

答案 1 :(得分:2)

在SQL Server中,您可以使用PIVOT函数生成结果:

select gender,
  Stage1, 
  Stage2
from
(
  select 
    c.gender,
    'Stage'+cast(stage as varchar(10)) Stage
  from (values ('F'),('M')) c (gender)
  left join tblpersonaldata p
    on c.gender = p.gender
  left join tblStudentsadmitted s
    on p.uid = s.uid
    and s.Status='Y'
)src
pivot
(
  count(stage)
  for stage in (Stage1, Stage2)
) piv

请参阅SQL Fiddle with Demo

由于您使用的是SQL Server 2008,因此此查询使用VALUES生成您在最终结果集中所需的性别列表

from (values ('F'),('M')) c (gender)

然后在其他表格上使用LEFT JOIN,最终结果将为MF值返回一行。

这也可以使用UNION ALL来编写,以生成性别列表:

select gender,
  Stage1, 
  Stage2
from
(
  select 
    c.gender,
    'Stage'+cast(stage as varchar(10)) Stage
  from 
  (
    select 'F' gender union all
    select 'M' gender
  ) c
  left join tblpersonaldata p
    on c.gender = p.gender
  left join tblStudentsadmitted s
    on p.uid = s.uid
    and s.Status='Y'
)src
pivot
(
  count(stage)
  for stage in (Stage1, Stage2)
) piv

请参阅SQL Fiddle with Demo

两者的结果是:

| GENDER | STAGE1 | STAGE2 |
----------------------------
|      F |      0 |      0 |
|      M |      2 |      1 |

答案 2 :(得分:1)

这也有效。将Left joins与新表格一起使用(包含两个性别记录的表格M&amp; F )。

<强> Fiddle demo

select  t.g Gender,
        isnull(sum(case when Stage = 1 then 1 end),0) Stage1,
        isnull(sum(case when Stage = 2 then 1 end),0) Stage2
from    (values ('M'),('F')) t(g) 
            left join personal a on t.g = a.gender
            left join studentadmitted b on a.uid = b.uid and b.Status = 'Y'
group by t.g
order by t.g

| GENDER | STAGE1 | STAGE2 |
----------------------------
|      F |      0 |      0 |
|      M |      2 |      1 |

答案 3 :(得分:0)

SELECT GENDER,0 AS&#39; STAGE 0&#39;,1 AS&#39; STAGE 1&#39;,2 AS&#39; STAGE 2&#39;
 从 ( SELECT P.ID,GENDER,当阶段为空时的情况,然后是0阶段结束阶段 来自tblPersonaldata P. LEFT JOIN tblStudentsadmitted S ON P.UID = S.UID ) 作为一个 枢 ( 阶段的计数(ID)([0],[1],[2]) )P