SQL Server 2008 - 数据透视

时间:2014-09-17 23:15:12

标签: sql sql-server

需要帮助转动某些数据

SELECT 
   [contact_list_name] as 'Table'
   ,[ov_number_dialed] as 'Number Dialed'
   ,convert(date, [time_of_contact], 101) as 'Date'
   ,convert(time, dateadd(hour, -7,[time_of_contact]), 108) as 'Time'
   ,agent_full_name as 'Agent'
   ,[response_status] as 'Disp'
FROM 
   [meldb].[dbo].[cl_contact_event]

给我这样的结果:

enter image description here

我想转出或显示结果,以便disp位于

之类的行中

enter image description here

有什么想法?

1 个答案:

答案 0 :(得分:1)

根据您的评论,一种简单的转化方式是将maxcase一起使用:

select 
    [contact_list_name] as 'Table'
    , [ov_number_dialed] as 'Number Dialed'
    , convert(date, [time_of_contact], 101) as 'Date'
    , convert(time, dateadd(hour, -7,[time_of_contact]), 108) as 'Time'
    , agent_full_name as 'Agent'
    , max(case when [response_status] = 'DAM' then response_status end) as 'Disp1_DAM'
    , max(case when [response_status] = 'DNA' then response_status end) as 'Disp2_DNA'
    ...
FROM [meldb].[dbo].[cl_contact_event]
GROUP BY
    [contact_list_name] 
    , [ov_number_dialed] 
    , convert(date, [time_of_contact], 101) 
    , convert(time, dateadd(hour, -7,[time_of_contact]), 108) 
    , agent_full_name