如何在 SQL 中将使用 case 语句制作的 2 个新的附加列放在一起
How to put together 2 new additional columns made using a case statement together in SQL
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];
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];