多个case语句返回1列,包含多行

时间:2018-02-23 17:26:28

标签: sql multiple-columns alias unpivot case-when

我需要评估两组时间戳并创建一个可能有多行的列。为了使事情更复杂,我需要使用函数来收集这些时间戳。我想要的是什么:

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' END AS TimeGroup,
CASE WHEN [functionBStartTime] <= '05:59' AND [functionBStopTime] >= '00:00' THEN 'A' END AS TimeGroup,
CASE WHEN [functionAStartTime] <= '11:59' AND [functionAStopTime] >= '06:00' THEN 'B' END AS TimeGroup,
CASE WHEN [functionBStartTime] <= '11:59' AND [functionBStopTime] >= '06:00' THEN 'B' END AS TimeGroup,
CASE WHEN [functionAStartTime] <= '17:59' AND [functionAStopTime] >= '12:00' THEN 'C' END AS TimeGroup,
CASE WHEN [functionBStartTime] <= '17:59' AND [functionBStopTime] >= '12:00' THEN 'C' END AS TimeGroup,
CASE WHEN [functionAStartTime] <= '23:59' AND [functionAStopTime] >= '18:00' THEN 'D' END AS TimeGroup,
CASE WHEN [functionBStartTime] <= '23:59' AND [functionBStopTime] >= '18:00' THEN 'D' END AS TimeGroup

FROM

bmpi,
mpd

WHERE

mpd.pid = bmpi.pid
AND mpd.cec = bmpi.cec

每个CCN可以有一组或两组时间戳,并且任一组都可以跨越多个组。这显然会创建多个列(Time_Group,Time_Group_1,Time_Group_3等)我需要一个&#34;时间组&#34;每个结果都有一个单独行的列。如:

CCN   |  Date    | A_Start | A_Stop | B_Start | B_Stop | Facility | Module | Time Group
1234  | 01/01/01 | 07:00   | 12:00  | 17:00   | 21:00  | WPDH     | 0012   | B
1234  | 01/01/01 | 07:00   | 12:00  | 17:00   | 21:00  | WPDH     | 0012   | C
1234  | 01/01/01 | 07:00   | 12:00  | 17:00   | 21:00  | WPDH     | 0021   | C
1234  | 01/01/01 | 07:00   | 12:00  | 17:00   | 21:00  | WPDH     | 0021   | D
4321  | 02/02/02 | 02:00   | 03:00  | 13:00   | 14:00  | ABCD     | 0012   | A
4321  | 02/02/02 | 02:00   | 03:00  | 13:00   | 14:00  | ABCD     | 0021   | C
0001  | 03/03/03 | 06:00   | 08:00  | NULL    | NULL   | WPDH     | 0012   | B

我一直在教我自己在需要的时候寻找我需要的东西,所以非常感谢一个例子和解释。

1 个答案:

答案 0 :(得分:0)

因此,CASE语句是一种在SQL中执行“if - then - else”逻辑的方法,而不是获取多个结果的方法。

我认为您需要一个子查询,将每个时间组值作为单独的列,然后主查询将UNPIVOT这些列。这样的事情可能有用:

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