由参数决定的列名

问题描述 投票:0回答:1

两种方法我都试过了。第一个有语法错误。第二个推送结果中的每一列,而不仅仅是与@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'
tsql case
1个回答
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查询:

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

结果:

上面的代码会选择列名如下:

  • WHEN 1 THEN 'DIR'
  • 当 2 然后 'DD'
  • 当 3 时“OD_Staff”
  • 当 4 THEN 'DI_BC'
  • WHEN 5 THEN 'DI_PLs'
  • 当 6 时“DI_PQAs”
  • WHEN 7 THEN 'DI_FTEs'
  • 8 点时“AIPQBBC”
  • 9 时,然后是“AIPQB_PL”
  • 当 10 时“AIPQB_PQA”
  • 11 时,然后是“AIPQB_GS13S”
  • 当 12 时,然后是“AIPQB_FTE”
  • 13 岁时,“IT_Staff”
  • 14 岁时“IT_Sup”
© www.soinside.com 2019 - 2024. All rights reserved.