Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CASE to create new column based on specific text in a string column

Tags:

I found a couple similar threads, but none are working. I'm trying to create a new column for when another column satisfies a certain condition.

This is the code I'm working with:

SELECT DISTINCT R.[Column1] AS Person, 
SUM(CASE WHEN  R.[Event] = 'Event1' THEN 1 ELSE NULL END) AS Event1,
    CASE (WHEN L.[Column2] LIKE '%String1%' THEN 'String1'
        ELSE WHEN L.[Column2] LIKE '%String2%' THEN 'String2'
        ELSE WHEN L.[Column2] LIKE '%String3%' THEN 'String3'
        ELSE NULL END) AS NewColumn
  FROM [Database1].[dbo].[Table1] R
  LEFT JOIN
     [Database1].[dbo].[Table2] L
        ON R.[UniqueIdentifier] = L.[UniqueIdentifier]
    WHERE L.[Column2] LIKE '%String1%'
        OR L.[Column2] LIKE '%String2%'
        OR L.[Column2] LIKE '%String3%'
GROUP BY  R.[Column1], L.[Column2]
ORDER BY R.[Event1] DESC

If I take the CASE statements from column 2 out, then the query works fine. My desired results are three columns: Person, String, Event... counting Events with an aggregation on Person and String.

The error is: Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'CASE'.

like image 535
Nathan Anderson Avatar asked Jul 10 '17 17:07

Nathan Anderson


People also ask

How do you create a new column in SQL based on another column?

In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table.

Does a case statement create a column?

CASE WHEN takes in values, checks them against a condition and THEN outputs values into a new column based on if it satisfies the condition. Note: CASE WHEN statements will always output new values to a new column which is different than “if then” which can replace values in the same column.

Can we use CASE statement in insert query?

Insert statement with CASEYou can use the CASE expression to insert data into a SQL Server table. The INSERT statement with CASE will scan for the required values and if found, insert values from THEN expression.

How do I do an IF THEN statement in SQL?

Syntax. IF (a <= 20) THEN c:= c+1; END IF; If the Boolean expression condition evaluates to true, then the block of code inside the if statement will be executed. If the Boolean expression evaluates to false, then the first set of code after the end of the if statement (after the closing end if) will be executed.


1 Answers

You had some syntax issues:

1st issue was: CASE (WHEN

2nd issue was: ELSE WHEN

This should run fine now:

SELECT DISTINCT
       R.[Column1] AS Person,
       SUM(CASE
               WHEN R.[Event] = 'Event1'
               THEN 1
               ELSE NULL
           END) AS Event1,
       (CASE
            WHEN L.[Column2] LIKE '%String1%'
            THEN 'String1'
            WHEN L.[Column2] LIKE '%String2%'
            THEN 'String2'
            WHEN L.[Column2] LIKE '%String3%'
            THEN 'String3'
            ELSE NULL
        END) AS NewColumn
FROM [Database1].[dbo].[Table1] R
     LEFT JOIN [Database1].[dbo].[Table2] L ON R.[UniqueIdentifier] = L.[UniqueIdentifier]
WHERE L.[Column2] LIKE '%String1%'
      OR L.[Column2] LIKE '%String2%'
      OR L.[Column2] LIKE '%String3%'
GROUP BY R.[Column1],
         L.[Column2]
ORDER BY R.[Event1] DESC;
like image 166
Fuzzy Avatar answered Oct 11 '22 12:10

Fuzzy