我有一个类似
的查询SELECT weeks_ind, weeks_rol, weeks_no, date,
CASE WHEN SUM(CASE WHEN rec = 'Y' THEN 1 ELSE 0 END) >= 1 THEN 'Y' ELSE 'N' END "rec",
CASE WHEN (SUM( CASE WHEN glob is null THEN 1 WHEN glob = Id THEN 2 ELSE 0 END )) = 3 THEN 'Many'
WHEN (SUM( CASE WHEN glob is null THEN 1 WHEN glob = Id THEN 2 ELSE 0 END )) = 2 THEN 'One'
ELSE 'None' END "Type"
FROM Table WHERE Id = '12345'
AND ((visible = 'Y' AND rec = 'N') OR rec = 'Y') AND days = 100
GROUP BY date, weeks_no, weeks_ind, weeks_rol
ORDER BY date, weeks_no;
并且按预期效果运行良好,返回所有记录。 但是,如果我将“ rec”添加为GROUP
GROUP BY date, weeks_no, weeks_ind, weeks_rol, rec
查询不会返回所有记录,并且更多的是,类型错误。 任何想法为什么会这样?我知道该小组仅对记录进行分组,而不会影响记录的数量。
答案 0 :(得分:1)
rec
中的GROUP BY
是表中的那个,而不是查询中被简化为'Y'/'N'的那个。
您需要输入内部查询,例如:
SELECT weeks_ind, weeks_rol, weeks_no, date,
CASE WHEN (SUM( CASE WHEN glob is null THEN 1 WHEN glob = Id THEN 2 ELSE 0 END )) = 3 THEN 'Many'
WHEN (SUM( CASE WHEN glob is null THEN 1 WHEN glob = Id THEN 2 ELSE 0 END )) = 2 THEN 'One'
ELSE 'None' END "Type"
from (
SELECT weeks_ind, weeks_rol, weeks_no, date,
CASE WHEN SUM(CASE WHEN rec = 'Y' THEN 1 ELSE 0 END) >= 1 THEN 'Y' ELSE 'N' END "rec"
FROM Table WHERE Id = '12345'
AND ((visible = 'Y' AND rec = 'N') OR rec = 'Y') AND days = 100
GROUP BY date, weeks_no, weeks_ind, weeks_rol
) group by date, weeks_no, weeks_ind, weeks_rol, rec
ORDER BY date, weeks_no;