3

我有一张桌子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

4 回答 4

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 ║
╚════════╩════════╩════════╝
于 2013-03-12T11:39:19.677 回答
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在其他表上使用 a ,最终结果将为MF值返回一行。

这也可以使用 aUNION 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

两者的结果是:

| GENDER | STAGE1 | STAGE2 |
----------------------------
|      F |      0 |      0 |
|      M |      2 |      1 |
于 2013-03-12T11:47:22.613 回答
1

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

小提琴演示

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 |
于 2013-03-12T11:55:28.453 回答
0

SELECT GENDER, 0 AS 'STAGE 0', 1 AS 'STAGE 1', 2 AS 'STAGE 2'
FROM ( SELECT P.ID, GENDER,CASE WHEN STAGE IS NULL THEN 0 ELSE STAGE END STAGE FROM tblPersonaldata P LEFT JOIN tblStudentsadmitted S ON P.UID = S.UID ) 作为 PIVOT ( COUNT (ID) FOR STAGE IN ([0],[1],[2]) )P

于 2013-03-12T12:15:34.650 回答