因此,CASE语句是SQL中执行“if-then-else”逻辑的一种方式,而不是获得多个结果的方式。
我认为您需要一个子查询,该子查询将每个时间组值作为单独的列,然后主查询将取消对这些列的IVOT。类似的方法可能会奏效:
SELECT
CCN,
[Date],
A_Start,
A_Stop,
B_Start,
B_Stop,
Facility,
Module,
TimeGroup,
TimeGroupValues
FROM
(
SELECT DISTINCT
mpd.ccn AS CCN,
mpd.date AS [Date],
[functionAStartTime] AS A_Start,
[functionAStopTime] AS A_Stop,
[functionBStartTime] AS B_Start,
[functionBStopTime] AS B_Stop,
mpd.fac AS Facility,
bmpi.mod AS Module,
CASE
WHEN [functionAStartTime] <= '05:59' AND [functionAStopTime] >= '00:00'
THEN 'A'
WHEN [functionBStartTime] <= '05:59' AND [functionBStopTime] >= '00:00'
THEN 'A'
END AS TimeGroupA,
CASE
WHEN [functionAStartTime] <= '11:59' AND [functionAStopTime] >= '06:00'
THEN 'B'
WHEN [functionBStartTime] <= '11:59' AND [functionBStopTime] >= '06:00'
THEN 'B'
END AS TimeGroupB,
CASE
WHEN [functionAStartTime] <= '17:59' AND [functionAStopTime] >= '12:00'
THEN 'C'
WHEN [functionBStartTime] <= '17:59' AND [functionBStopTime] >= '12:00'
THEN 'C'
END AS TimeGroupC,
CASE
WHEN [functionAStartTime] <= '23:59' AND [functionAStopTime] >= '18:00'
THEN 'D'
WHEN [functionBStartTime] <= '23:59' AND [functionBStopTime] >= '18:00'
THEN 'D'
END AS TimeGroupD
FROM
bmpi
JOIN
mpd
ON
mpd.pid = bmpi.pid
AND
mpd.cec = bmpi.cec
) AS u
UNPIVOT
(TimeGroup FOR TimeGroupValues IN
(
TimeGroupA,
TimeGroupB,
TimeGroupC,
TimeGroupD
)
) AS p