如何将使用case语句在SQL中创建的2个新的附加列放在一起

问题描述 投票:0回答:1
SELECT 
    ScheduledStartDate, ActualStartDate, ScheduledEndDate, ActualEndDate,
    CASE 
       (WHEN ActualStartDate > ScheduledStartDate THEN 'started late'
        WHEN ActualStartDate < ScheduledStartDate THEN 'started early'
        WHEN ScheduledStartDate = ActualStartDate then 'started on time'
    END AS startDescription )
    ELSE
    CASE 
      (WHEN ActualEndDate < ScheduledEndDate THEN 'finished late'
       WHEN ActualEndDate > ScheduledEndDate THEN 'finished early'
       WHEN ActualEndDate = ScheduledEndDate THEN 'finished on time'
    END AS endDescription )
FROM
    [Production].[WorkOrderRouting];

我已创建4列,并希望将2个由'case'语句构成的新列加在一起。

sql case-statement
1个回答
1
投票

将每个CASE表达式放在单独的列中:

SELECT
    ScheduledStartDate,
    ActualStartDate,
    ScheduledEndDate,
    ActualEndDate,
    CASE WHEN ActualStartDate > ScheduledStartDate THEN 'started late'
         WHEN ActualStartDate < ScheduledStartDate THEN 'started early'
         WHEN ActualStartDate = ScheduledStartDate THEN 'started on time' END AS startDescription,
    CASE WHEN ActualEndDate < ScheduledEndDate THEN 'finished late'
         WHEN ActualEndDate > ScheduledEndDate THEN 'finished early'
         WHEN ActualEndDate = ScheduledEndDate THEN 'finished on time' END AS endDescription
FROM [Production].[WorkOrderRouting];
© www.soinside.com 2019 - 2024. All rights reserved.