获取每个Key的输出列

时间:2017-11-16 10:43:57

标签: sql sql-server tsql

我有一个大致有这个字段的数据库:

SELECT [UserId]
  ,[MissionId]
  ,[LevelId]
  ,[Key]
  ,[BooleanAnswer]
  ,[CreationDateTimeUtc]
  ,[QuestionId]
  FROM [Bakman].[dbo].[LevelEntries]
  WHERE LevelId = 2 AND QuestionId = 1 OR QuestionId = 2 OR QuestionId = 3 OR QuestionId = 4 OR QuestionId = 5 OR QuestionId = 6

现在我得到了这种输出:

UserId  MissionId   LevelId Key BooleanAnswer   CreationDateTimeUtc QuestionId
0      1            2       1   1               2017-10-07 11:39:26.350 1
0      1            2       2   1               2017-10-07 11:39:26.350 1
0      1            2       3   0               2017-10-07 11:39:26.350 1
0      1            2       4   1               2017-10-07 11:39:26.350 1
0      1            2       5   0               2017-10-07 11:39:26.350 1
0      1            2       6   1               2017-10-07 11:39:26.350 1

但现在我想为每个QuestionId获取一个列(等于[key])。

像这样:

       UserId   MissionId   LevelId Key1  Key2  Key3   Key4   Key5   Key6       CreationDateTimeUtc     QuestionId
      1         2       1     0     1     1     0      1      1      1          2017-10-07 11:39:26.350 1

其中Key [key]具有布尔awnser和属于该元素的键。 很高兴知道Userid + MissionId + LevelId + Key是此表的Composite键。

编辑:

尝试两个给定的答案我现在得到类似的东西

    UserId  MissionId   LevelId Key1    Key2    Key3    Key4    Key5    Key6
   1    1   2   1   NULL    NULL    NULL    NULL    NULL    
    1   1   2   NULL    1   NULL    NULL    NULL    NULL    
    1   1   2   NULL    NULL    0   NULL    NULL    NULL    
    1   1   2   NULL    NULL    NULL    1   NULL    NULL    
    1   1   2   NULL    NULL    NULL    NULL    1   NULL    
    1   1   2   NULL    NULL    NULL    NULL    NULL    1   

现在我想将这些结合起来,以便得到用户的等级结果。

2 个答案:

答案 0 :(得分:1)

尝试以下查询

SELECT
  UserId,
  MissionId,
  LevelId,
  MAX(CASE WHEN [Key]=1 THEN BooleanAnswer END) Key1,
  MAX(CASE WHEN [Key]=2 THEN BooleanAnswer END) Key2,
  MAX(CASE WHEN [Key]=3 THEN BooleanAnswer END) Key3,
  MAX(CASE WHEN [Key]=4 THEN BooleanAnswer END) Key4,
  MAX(CASE WHEN [Key]=5 THEN BooleanAnswer END) Key5,
  MAX(CASE WHEN [Key]=6 THEN BooleanAnswer END) Key6,
  CreationDateTimeUtc,
  QuestionId
FROM LevelEntries
WHERE LevelId=2
  AND QuestionId IN(1,2,3,4,5,6)
GROUP BY UserId,MissionId,LevelId,CreationDateTimeUtc,QuestionId

第二个变种

SELECT
  UserId,
  MissionId,
  LevelId,
  MAX(CASE WHEN [Key]=1 THEN BooleanAnswer END) Key1,
  MAX(CASE WHEN [Key]=2 THEN BooleanAnswer END) Key2,
  MAX(CASE WHEN [Key]=3 THEN BooleanAnswer END) Key3,
  MAX(CASE WHEN [Key]=4 THEN BooleanAnswer END) Key4,
  MAX(CASE WHEN [Key]=5 THEN BooleanAnswer END) Key5,
  MAX(CASE WHEN [Key]=6 THEN BooleanAnswer END) Key6,
  MAX(CreationDateTimeUtc) MaxCreationDateTimeUtc,
  QuestionId
FROM LevelEntries
WHERE LevelId=2
  AND QuestionId IN(1,2,3,4,5,6)
GROUP BY UserId,MissionId,LevelId,QuestionId

答案 1 :(得分:1)

您可以将PIVOT用于此目的。

    with Entries as
    (
        select UserId,
            MissionId,
            LevelId,
            'Key' + cast([Key] as varchar) as [Key],
            BooleanAnswer,
            CreationDateTimeUtc,
            QuestionId
        from [Bakman].[dbo].[LevelEntries]
        where LevelId = 2 and QuestionId in (1, 2, 3, 4, 5, 6)
    )

    select UserId,
        MissionId,
        LevelId,
        Key1, 
        Key2, 
        Key3, 
        Key4, 
        Key5,
        Key6,
        CreationDateTimeUtc,
        QuestionId
    from Entries t
    pivot
    (
        max(BooleanAnswer)
        for [Key] in (Key1, Key2, Key3, Key4, Key5, Key6)
    ) p;