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'语句构成的新列加在一起。
将每个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];