1

嗨,我在编写查询时需要帮助。

表格是:

tblStandard1students
tblStandard2students
tblStandard1students
tblDivision
tblCandidateinfo

tblStandard1students,tblStandard2students,tblStandard1studentstbl 包含有关参加标准 1,2 和 3 的学生的信息

tblStandrs1students

Candid  admitted
  1        Y
  2        N
  3        Y

tblDivision 仅包含 2 列

 ID     Division
  1       A
  2       B
  3       C

tbl候选人信息

Candid  gender Division
  1       M      1
  2       F      2

等等...

现在我想要这样的桌子

Division  Students(Standard1)  Students(Standard2)  Students(Standard3)
           M           F         M          F        M          F
------------------------------------------------------------------------
 A         1           0         0          0         0         1
 B         2           2         3          3         4         4
 C         1           0         0          0         0         0

我尝试了以下查询:

SELECT Division,

    ( SELECT count(*)
     FROM tblStandard1students A
     INNER JOIN tblCandidateinfo B ON A.Candid=B.Candid
     INNER JOIN tblDivision C ON C.ID=B.Division) AS Students(Standard1),

    ( SELECT count(*)
     FROM tblStandard2students A
     INNER JOIN tblCandidateinfo B ON A.Candid=B.Candid
     INNER JOIN tblDivision C ON C.ID=B.Division) AS Students(Standard2),

    ( SELECT count(*)
     FROM tblStandard3students A
     INNER JOIN tblCandidateinfo B ON A.Candid=B.Candid
     INNER JOIN tblDivision C ON C.ID=B.Division ) AS Students(Standard3)
FROM tblDivision Z

但这只是我按性别区分的查询的一半……帮助我完成它。

4

2 回答 2

2
;WITH combined AS
(
    SELECT ci.Division, 'Students(Standard1) ' + ci.gender AS grp
    FROM tblCandidateInfo ci
    INNER JOIN tblStandard1students s ON ci.Candid = s.Candid
    UNION ALL
    SELECT ci.Division, 'Students(Standard2) ' + ci.gender AS grp
    FROM tblCandidateInfo ci
    INNER JOIN tblStandard2students s ON ci.Candid = s.Candid
    UNION ALL
    SELECT ci.Division, 'Students(Standard3) ' + ci.gender AS grp
    FROM tblCandidateInfo ci
    INNER JOIN tblStandard1studentstbl s ON ci.Candid = s.Candid
)
SELECT Division, 
    [Students(Standard1) M], [Students(Standard1) F],
    [Students(Standard2) M], [Students(Standard2) F],
    [Students(Standard3) M], [Students(Standard3) F]
FROM
(
    SELECT d.Division, grp
    FROM tblDivision d
    LEFT OUTER JOIN combined c ON d.ID = c.Division
) x
PIVOT
(
    COUNT(grp)
    FOR grp IN ([Students(Standard1) M], [Students(Standard1) F],
        [Students(Standard2) M], [Students(Standard2) F],
        [Students(Standard3) M], [Students(Standard3) F])
) y
ORDER BY Division
于 2013-03-05T12:46:56.573 回答
0

选择 divison.Division ,IFNULL(stander1.M,0),IFNULL(stander1.F,0) FROM testtblDivisiondivison Left join (SELECT division ,count(case gender when 'M' then 1 else null end) as M,count(case gender when 'F' then 1 else null end) as F FROM .tc inner join test.tblStandars1students testts1tblCandidateinfo

ON tc.Candid=ts1.Candid

group by division) as stander1 on stander1.division= divison.id

按 divison.id 分组;

Insted of IFNULL 使用 ISNULL 并对所有标准表进行左连接

于 2013-03-06T06:08:40.607 回答