Search code examples
t-sqlcase

Column name determined by parameter


I've tried both approaches. First one has a syntax error. Second one shoves every column in the result instead of just the one that has a match with @LabelID.

    SELECT  (SELECT CASE @LabelID
         WHEN 1 THEN count(h.ee_cmn_idfr) as 'DIR'
         WHEN 2 THEN count(h.ee_cmn_idfr) as 'DD'
         WHEN 3 THEN count(h.ee_cmn_idfr) as 'OD_Staff'
         WHEN 4 THEN count(h.ee_cmn_idfr) as 'DI_BC'
         WHEN 5 THEN count(h.ee_cmn_idfr) as 'DI_PLs'
         WHEN 6 THEN count(h.ee_cmn_idfr) as 'DI_PQAs'
         WHEN 7 THEN count(h.ee_cmn_idfr) as 'DI_FTEs'
         WHEN 8 THEN count(h.ee_cmn_idfr) as 'AIPQBBC'
         WHEN 9 THEN count(h.ee_cmn_idfr) as 'AIPQB_PL'
         WHEN 10 THEN count(h.ee_cmn_idfr) as 'AIPQB_PQA'
         WHEN 11 THEN count(h.ee_cmn_idfr) as 'AIPQB_GS13S'
         WHEN 12 THEN count(h.ee_cmn_idfr) as 'AIPQB_FTE'
         WHEN 13 THEN count(h.ee_cmn_idfr) as 'IT_Staff'
         WHEN 14 THEN count(h.ee_cmn_idfr) as 'IT_Sup'
        )

second approach:

SELECT
 CASE WHEN @LabelID = 1 THEN count(h.ee_cmn_idfr) as 'DIR'
,CASE WHEN @LabelID = 2 THEN count(h.ee_cmn_idfr) END as 'DD'
,CASE WHEN @LabelID = 3 THEN count(h.ee_cmn_idfr) END as 'OD_Staff'
,CASE WHEN @LabelID = 4 THEN count(h.ee_cmn_idfr) END as 'DI_BC'
,CASE WHEN @LabelID = 5 THEN count(h.ee_cmn_idfr) END as 'DI_PLs'
,CASE WHEN @LabelID = 6 THEN count(h.ee_cmn_idfr) END as 'DI_PQAs'
,CASE WHEN @LabelID = 7 THEN count(h.ee_cmn_idfr) END as 'DI_FTEs'
,CASE WHEN @LabelID = 8 THEN count(h.ee_cmn_idfr) END as 'AIPQBBC'
,CASE WHEN @LabelID = 9 THEN count(h.ee_cmn_idfr) END as 'AIPQB_PL'
,CASE WHEN @LabelID = 10 THEN count(h.ee_cmn_idfr) END as 'AIPQB_PQA'
,CASE WHEN @LabelID = 11 THEN count(h.ee_cmn_idfr) END as 'AIPQB_GS13S'
,CASE WHEN @LabelID = 12 THEN count(h.ee_cmn_idfr) END as 'AIPQB_FTE'
,CASE WHEN @LabelID = 13 THEN count(h.ee_cmn_idfr) END as 'IT_Staff'
,CASE WHEN @LabelID = 14 THEN count(h.ee_cmn_idfr) END as 'IT_Sup'

Solution

  • Here is how you would do it using dynamic SQL:

    SAMPLE DATA:

    IF OBJECT_ID('tempdb..#INPUT') IS NOT NULL
        DROP TABLE #INPUT;
    
    CREATE TABLE #INPUT(RowID       INT IDENTITY(1, 1)
                   , ee_cmn_idfr INT);
    
    INSERT INTO       #INPUT(ee_cmn_idfr)
    VALUES
          (1),
          (1),
          (1),
          (1),
          (1),
          (1),
          (1),
          (1),
          (1),
          (1),
          (1),
          (1);
    

    SQL QUERY:

    DECLARE @LabelID INT = 1; --<-- set the labelID 
    
    DECLARE @SQL NVARCHAR(MAX) = ''; --<-- declare a variable to hold the dynamic sql
    
    SELECT @SQL = 'SELECT COUNT(ee_cmn_idfr) AS '+QUOTENAME(CASE @LabelID
                                                    WHEN 1 THEN 'DIR'
                                                    WHEN 2 THEN 'DD'
                                                    WHEN 3 THEN 'OD_Staff'
                                                    WHEN 4 THEN 'DI_BC'
                                                    WHEN 5 THEN 'DI_PLs'
                                                    WHEN 6 THEN 'DI_PQAs'
                                                    WHEN 7 THEN 'DI_FTEs'
                                                    WHEN 8 THEN 'AIPQBBC'
                                                    WHEN 9 THEN 'AIPQB_PL'
                                                    WHEN 10 THEN 'AIPQB_PQA'
                                                    WHEN 11 THEN 'AIPQB_GS13S'
                                                    WHEN 12 THEN 'AIPQB_FTE'
                                                    WHEN 13 THEN 'IT_Staff'
                                                    WHEN 14 THEN 'IT_Sup'
                                                 END)+' FROM #INPUT'; --<-- you'll have to change the name of the table accordingly 
    
    PRINT(@SQL); --<-- print out the query not needed but nice to have for debuging 
    
    EXEC (@SQL); --<-- execute the dynamic sql
    

    the PRINT(@SQL) will print the following:

    SELECT COUNT(ee_cmn_idfr) AS [DIR] FROM #INPUT
    

    RESULTS:

    enter image description here

    the above code will select the column name as follows:

    • WHEN 1 THEN 'DIR'
    • WHEN 2 THEN 'DD'
    • WHEN 3 THEN 'OD_Staff'
    • WHEN 4 THEN 'DI_BC'
    • WHEN 5 THEN 'DI_PLs'
    • WHEN 6 THEN 'DI_PQAs'
    • WHEN 7 THEN 'DI_FTEs'
    • WHEN 8 THEN 'AIPQBBC'
    • WHEN 9 THEN 'AIPQB_PL'
    • WHEN 10 THEN 'AIPQB_PQA'
    • WHEN 11 THEN 'AIPQB_GS13S'
    • WHEN 12 THEN 'AIPQB_FTE'
    • WHEN 13 THEN 'IT_Staff'
    • WHEN 14 THEN 'IT_Sup'