以下是MAX CASE WHEN的SQL查询...
SELECT dbo.icalificaciones.idalumno,
dbo.ialumnosins.appaterno + ' ' + dbo.ialumnosins.apmaterno + ' ' + dbo.ialumnosins.nombre AS Alumnos,
MAX(CASE WHEN idparcial = 1 THEN calfinal ELSE null END) AS U1,
MAX(CASE WHEN idparcial = 2 THEN calfinal ELSE null END) AS U2,
MAX(CASE WHEN idparcial = 3 THEN calfinal ELSE null END) AS U3,
MAX(CASE WHEN idparcial = 4 THEN calfinal ELSE null END) AS U4,
MAX(CASE WHEN idparcial = 5 THEN calfinal ELSE null END) AS U5,
MAX(CASE WHEN idparcial = 6 THEN calfinal ELSE null END) AS U6,
MAX(CASE WHEN idparcial = 7 THEN calfinal ELSE null END) AS U7,
MAX(CASE WHEN idparcial = 8 THEN calfinal ELSE null END) AS U8
FROM dbo.icalificaciones INNER JOIN dbo.ialumnosins ON dbo.icalificaciones.idalumno = dbo.ialumnosins.IDalumno
这是结果。 如何删除空值? 我不需要U2,U3,U4 ...... U8。 感谢。
答案 0 :(得分:0)
您正在寻找使用HAVING关键字。
假设你想省略并记录将列U2返回为NULL的记录。你只需使用:
HAVING MAX(CASE WHEN idparcial = 2 THEN calfinal ELSE null END) is not null
在您的查询结束时。