SQL Server:查询以透视并显示未设置的值

SQL Server:查询以透视并显示未设置的值,sql,sql-server,report,Sql,Sql Server,Report,我有一个SQL Server表,有3列:UserID、SettingID、SettingValue 范例 UserID | SettingID | Value 1 | 10 |0 1 | 11 |1 1 | 14 |0 2 | 10 |1 2 | 13 |1 需要根据设置ID转换为列 可能是没有设置ID的行->想要获取不存在的并显示为“未设置” 预期结果: UserID |

我有一个SQL Server表,有3列:
UserID、SettingID、SettingValue

范例

UserID | SettingID | Value
1      | 10        |0
1      | 11        |1
1      | 14        |0
2      | 10        |1
2      | 13        |1
  • 需要根据设置ID转换为列
  • 可能是没有设置ID的行->想要获取不存在的并显示为“未设置”
  • 预期结果:

    UserID | Setting10 | Setting11 | Setting13 | Setting14
    1      |  Off      |  On       |  not set  |  off
    2      |  On       |  not set  |  on       |  not set
    
    设置ID列表已给出,无需分析和自动查找


    不知道如何处理这个问题,因为你说设置ID的列表是固定的,你可以通过条件聚合得到想要的结果

    select userid,
    max(case when settingid = '10' then 
                                       case when value = 0 then 'Off'
                                            when value = 1 then 'On'
                                       else 'not set' end
        end) as setting10,
    --proceed similarly for other settings
    from yourtable
    group by userid;
    

    MS SQL Server 2008架构设置

    DECLARE @Table TABLE (UserID INT,  SettingID INT, Value INT )
    INSERT INTO @Table VALUES 
    (1      , 10        ,0),
    (1      , 11        ,1),
    (1      , 14        ,0),
    (2      , 10        ,1),
    (2      , 13        ,1)
    
    | UserID | Setting10 | Setting11 | Setting13 | Setting14 |
    |--------|-----------|-----------|-----------|-----------|
    |      1 |       off |        on |   not set |       off |
    |      2 |        on |   not set |        on |   not set |
    
    查询1

    SELECT UserID
           ,COALESCE(  Setting10 , 'not set') Setting10 
           ,COALESCE(  Setting11 , 'not set') Setting11 
           ,COALESCE(  Setting13 , 'not set') Setting13
           ,COALESCE(  Setting14 , 'not set') Setting14
    FROM (
        SELECT UserID 
             , 'Setting' + CAST(SettingID AS VARCHAR(10)) AS Settings
             ,CASE Value WHEN 0 THEN 'off'
                         WHEN 1 THEN 'on'
                    END AS Value
        FROM @Table
        ) t
        PIVOT (MAX(Value)
               FOR Settings
               IN (Setting10 , Setting11 , Setting13 , Setting14)
              )p
    

    DECLARE @Table TABLE (UserID INT,  SettingID INT, Value INT )
    INSERT INTO @Table VALUES 
    (1      , 10        ,0),
    (1      , 11        ,1),
    (1      , 14        ,0),
    (2      , 10        ,1),
    (2      , 13        ,1)
    
    | UserID | Setting10 | Setting11 | Setting13 | Setting14 |
    |--------|-----------|-----------|-----------|-----------|
    |      1 |       off |        on |   not set |       off |
    |      2 |        on |   not set |        on |   not set |
    

    这是你问题的答案

    DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
    DECLARE @ColumnName AS NVARCHAR(MAX)
    DECLARE @ColumnCaseName AS NVARCHAR(MAX)
    
    --Get distinct values of the PIVOT Column 
    SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
       + QUOTENAME(SettingID)
    FROM (SELECT DISTINCT SettingID FROM TableName) AS TableName
    
    SELECT @ColumnCaseName= ISNULL(@ColumnCaseName + ',','') 
       + 'case when '+QUOTENAME(SettingID)+' = 0 then ''Off'' when  '+QUOTENAME(SettingID)+' = 1 then ''On''  else ''not set'' end  '
    + QUOTENAME(SettingID)
    FROM (SELECT DISTINCT SettingID FROM TableName) AS TableName
    
    --Prepare the PIVOT query using the dynamic 
    SET @DynamicPivotQuery = 
    N'SELECT UserID, ' + @ColumnCaseName + '
    FROM TableName
    PIVOT(Max(Value) 
          FOR SettingID  IN (' + @ColumnName + ')) AS PVTTable'
    --Execute the Dynamic Pivot Query
    EXEC sp_executesql @DynamicPivotQuery
    
    这将适用于您在表中添加的任意数量的行

    快乐编码