Sattu
Sattu

Reputation: 5

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];

I have made 4 columns and want to add 2 new columns made with 'case' statement together.

Upvotes: 0

Views: 33

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521073

Make each CASE expression a separate column:

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];

Upvotes: 1

Related Questions