列名由参数确定

时间:2016-10-06 19:34:42

标签: tsql case-when columnname

我尝试了两种方法。第一个有语法错误。第二个推送结果中的每一列,而不只是推送与@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'
        )

第二种方法:

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'

1 个答案:

答案 0 :(得分:1)

以下是使用动态SQL的方法:

示例数据:

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

PRINT(@SQL)将打印以下内容:

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

结果:

enter image description here

上面的代码将选择列名,如下所示:

  • 1,那么DIR&#39;
  • 2,那么DD&#39;
  • 3,然后&#39; OD_Staff&#39;
  • 4,那么&#39; DI_BC&#39;
  • 5,那么&#39; DI_PLs&#39;
  • 6,那么DI_PQAs&#39;
  • 7,那么&#39; DI_FTEs&#39;
  • 8,那么AIPQBBC&#39;
  • 9那么&#39; AIPQB_PL&#39;
  • 10岁那么&#39; AIPQB_PQA&#39;
  • 11岁那么&#39; AIPQB_GS13S&#39;
  • 12岁那么&#39; AIPQB_FTE&#39;
  • 13岁那么&#39; IT_Staff&#39;
  • 14岁那么&#39; IT_Sup&#39;