我想将两段代码合二为一,但出现错误:
第1部分
SELECT idstd,namestd, idmajor,
c1, c2, c3, c4, c5, c6,
c7, c8, c9, c10,c11,c12,
c13,c14,c15
FROM
(SELECT status, idstd,namestd, idmajor,
'C' + cast(row_number()
OVER (partition BY idstd, idmajor
ORDER BY (SELECT 1)) AS varchar(10)) col
FROM tbcheked) src
PIVOT (MAX(status) FOR col IN (C1, C2, C3, C4, C5,
C6, C7, C8, C9, C10,
c11,c12,c13,c14,c15)) piv
此代码用于检查列status
是否具有present
、absent
或的值leave
。
例子:
id | c1 | c2 | c3 | c4 | c5 |
1 present absent present leave present
我有第二段代码用于状态计数:
SELECT idstd,
namemajor,
SUM(CASE WHEN status = 'present'
THEN 1
ELSE 0
END) AS present,
SUM(CASE WHEN status = 'absent'
THEN 1
ELSE 0
END) AS absent,
SUM(CASE WHEN status = 'leave'
THEN 1
ELSE 0
END) AS leave,
FROM tbcheked GROUP BY idstd, namemajor
ORDER BY idstd
输出:
id | present | absent | leave |
1 3 1 1
现在,我想加入这两个查询:
id | c1 | c2 | c3 | c4 | c5 | present | absent | leave |
1 present absent present leave present 3 1 1