Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select statement inside CASE - SQL

I am trying to figure out the right code for the below logic. If there is a certain value in the first column, display the value from the second column for that specific record. Can someone please help? Thanks.

CASE WHEN TableA.Column1 = 'a' THEN 'select TableA.Column2 '
     WHEN TableA.Column4 = 'b' THEN 'select TableA.Column5'

     ELSE TableA.Column6
END AS [Test]
like image 453
Bulbul Avatar asked Feb 11 '23 06:02

Bulbul


1 Answers

You were almost there just remove the select from case statement. Since all the values are coming from same table no need of select in case statement just keep column name it will fetch the corresponding column value.

SELECT CASE
         WHEN Column1 = 'a' THEN Column2
         WHEN Column4 = 'b' THEN Column5
         ELSE Column6
       END AS [Test]
FROM   tableA 
like image 97
Pரதீப் Avatar answered Feb 13 '23 20:02

Pரதீப்