sql server 2017 - 如何创建唯一值并将列与这些值相关联

时间:2018-06-07 12:43:18

标签: sql sql-server

我是SQL服务器的新手,但发现它非常有用。

现在我正在与我需要重新排列的sql结构作斗争。

我有一个有6列的数据库:

Call ID and Question1, 2, 3, 4, 5, 6

呼叫ID是接听电话的人,因为询问了6个问题,同一个呼叫ID可以存在于呼叫ID列中的多个位置。

为了创建唯一的呼叫ID列表,我已经进行了查询:

SELECT DISTINCT [call_id]
FROM [ConnectCallData_26391].[dbo].[call_events] 

我现在需要做的事情:

我需要使用来自CALL ID DISCTINCT查询的结果,并将问题1-6与调用ID对齐,以便唯一的调用ID只有一行,所有问题如前所述都列在第2-6行!

非常需要帮助!!

亲切的问候丹尼尔

这是用于分隔问题的查询:

select 
      ,[menue_key]
      ,dte_start 
      ,[dte_updated]
      ,[rec_id]
      ,[call_id] 
      ,[duration_tot_sec] 
      ,iif([menue_key] ='SYS FLX HAC survey_question_1' OR [menue_key] ='SYS FLX Flextur survey_question_1',[menue_choice],'' ) as 'Hvad er din alder?'
      ,iif([menue_key] ='SYS FLX HAC survey_question_2' OR [menue_key] ='SYS FLX Flextur survey_question_2',[menue_choice],'' ) as 'Hvilket postnummer bor du i?'
      ,iif([menue_key] ='SYS FLX HAC survey_question_3' OR [menue_key] ='SYS FLX Flextur survey_question_3',[menue_choice],'' ) as 'Hvor tilfreds er du generelt med at benytte den anvendte ordning?'
      ,iif([menue_key] ='SYS FLX HAC survey_question_4' OR [menue_key] ='SYS FLX Flextur survey_question_4',[menue_choice],'' ) as 'Hvis du ikke havde den anvendte ordning til rådighed, hvad ville du så benytte som erstatning for dine rejser?'
      ,iif([menue_key] ='SYS FLX HAC survey_question_5' OR [menue_key] ='SYS FLX Flextur survey_question_5',[menue_choice],'' ) as 'Hvad er formålet med den tur du lige har bestilt?'
      ,iif([menue_key] ='SYS FLX HAC survey_question_6' OR [menue_key] ='SYS FLX Flextur survey_question_6',[menue_choice],'' )as 'Hvorfor bestiller du ikke din tur via internettet?'

  FROM [ConnectCallData_26391].[dbo].[call_events] 
  where menue_key in ('SYS FLX HAC survey_question_1','SYS FLX HAC survey_question_2', 'SYS FLX HAC survey_question_3',
  'SYS FLX HAC survey_question_4','SYS FLX HAC survey_question_5','SYS FLX HAC survey_question_6','SYS FLX Flextur survey_question_1','SYS FLX Flextur survey_question_2', 'SYS FLX Flextur survey_question_3',
  'SYS FLX Flextur survey_question_4','SYS FLX Flextur survey_question_5','SYS FLX Flextur survey_question_6')

1 个答案:

答案 0 :(得分:0)

你需要转动

;WITH Prepivot AS
(
    select 
          [call_id] AS Call_ID
          ,[menue_key]
          ,[menue_choice]
      FROM 
        [ConnectCallData_26391].[dbo].[call_events] 
      where 
        menue_key in (
            'SYS FLX HAC survey_question_1',
            'SYS FLX HAC survey_question_2', 
            'SYS FLX HAC survey_question_3',
            'SYS FLX HAC survey_question_4',
            'SYS FLX HAC survey_question_5',
            'SYS FLX HAC survey_question_6',
            'SYS FLX Flextur survey_question_1',
            'SYS FLX Flextur survey_question_2', 
            'SYS FLX Flextur survey_question_3',
            'SYS FLX Flextur survey_question_4',
            'SYS FLX Flextur survey_question_5',
            'SYS FLX Flextur survey_question_6')
)
SELECT
    P.*
FROM
    Prepivot AS N
    PIVOT (
        MAX(N.[menue_choice]) FOR N.[menue_key] IN (
            [SYS FLX HAC survey_question_1],
            [SYS FLX HAC survey_question_2], 
            [SYS FLX HAC survey_question_3],
            [SYS FLX HAC survey_question_4],
            [SYS FLX HAC survey_question_5],
            [SYS FLX HAC survey_question_6],
            [SYS FLX Flextur survey_question_1],
            [SYS FLX Flextur survey_question_2], 
            [SYS FLX Flextur survey_question_3],
            [SYS FLX Flextur survey_question_4],
            [SYS FLX Flextur survey_question_5],
            [SYS FLX Flextur survey_question_6])
    ) AS P

或条件聚合:

select 
      [call_id] 
      ,MAX(CASE WHEN  [menue_key] = 'SYS FLX HAC survey_question_1' OR [menue_key] ='SYS FLX Flextur survey_question_1' THEN [menue_choice] END) as 'Hvad er din alder?'
      ,MAX(CASE WHEN  [menue_key] = 'SYS FLX HAC survey_question_2' OR [menue_key] ='SYS FLX Flextur survey_question_2' THEN [menue_choice] END) as 'Hvilket postnummer bor du i?'
      ,MAX(CASE WHEN  [menue_key] = 'SYS FLX HAC survey_question_3' OR [menue_key] ='SYS FLX Flextur survey_question_3' THEN [menue_choice] END) as 'Hvor tilfreds er du generelt med at benytte den anvendte ordning?'
      ,MAX(CASE WHEN  [menue_key] = 'SYS FLX HAC survey_question_4' OR [menue_key] ='SYS FLX Flextur survey_question_4' THEN [menue_choice] END) as 'Hvis du ikke havde den anvendte ordning til rådighed, hvad ville du så benytte som erstatning for dine rejser?'
      ,MAX(CASE WHEN  [menue_key] = 'SYS FLX HAC survey_question_5' OR [menue_key] ='SYS FLX Flextur survey_question_5' THEN [menue_choice] END) as 'Hvad er formålet med den tur du lige har bestilt?'
      ,MAX(CASE WHEN  [menue_key] = 'SYS FLX HAC survey_question_6' OR [menue_key] ='SYS FLX Flextur survey_question_6' THEN [menue_choice] END) as 'Hvorfor bestiller du ikke din tur via internettet?'
FROM 
    [ConnectCallData_26391].[dbo].[call_events] 
where 
    menue_key in (
        'SYS FLX HAC survey_question_1',
        'SYS FLX HAC survey_question_2', 
        'SYS FLX HAC survey_question_3',
        'SYS FLX HAC survey_question_4',
        'SYS FLX HAC survey_question_5',
        'SYS FLX HAC survey_question_6',
        'SYS FLX Flextur survey_question_1',
        'SYS FLX Flextur survey_question_2', 
        'SYS FLX Flextur survey_question_3',
        'SYS FLX Flextur survey_question_4',
        'SYS FLX Flextur survey_question_5',
        'SYS FLX Flextur survey_question_6')
GROUP BY
    [call_id]