Home > other >  How to concatenate the result of columns to a new column
How to concatenate the result of columns to a new column

Time:12-15

How to concatenate the result of those columns to be the final result:

SELECT 
    ID, 
    CASE
        WHEN t1.SCORE <= 10 THEN '2A'
        WHEN (t1.SCORE > 20 AND t1.SCORE <= 30) THEN '2B'
        WHEN t1.SCORE > 30 THEN '2C'
    END AS Result1,
    CASE
        WHEN t2.POINT <= 10 THEN '2A'
        WHEN (t2.POINT > 20 AND t2.POINT <= 30) THEN '2B'
        WHEN t2.POINT > 30 THEN '2C'
    END AS Result2,

    -- CONCAT(Result1,Result2) AS FinalResult
FROM 
    Table1 t1 
INNER JOIN 
    Table2 t2 ON t2.CustomerID = t1.ID

How can I add an expression like this:

CONCAT(Result1, Result2) AS FinalResult

CodePudding user response:

Wrap your query up as a derived table:

select Result1, Result2, CONCAT(Result1,Result2) AS FinalResult
from
(
    SELECT ID, 
            CASE
                WHEN t1.SCORE <= 10 THEN '2A'
                WHEN (t1.SCORE > 20 AND t1.SCORE <= 30) THEN '2B'
                WHEN t1.SCORE > 30 THEN '2C'
            END AS Result1,
    
            CASE
                WHEN t2.POINT <= 10 THEN '2A'
                WHEN (t2.POINT > 20 AND t2.POINT <= 30) THEN '2B'
                WHEN t2.POINT > 30 THEN '2C'
            END AS Result2

    FROM Table1 t1 INNER JOIN Table2 t2 ON t2.CustomerID = t1.ID
) dt
  • Related