如何将重复列转换为行?

时间:2013-01-22 19:09:32

标签: sql sql-server

我有一个表,在一列中包含具有唯一值的行。如何将这些放在一行中,以便一个条目有多个条目?

例如,这是一张表...

create table visit_view
(
  last_name varchar(25),
  first_name varchar(25),
  middle_name varchar(25),
  dob datetime,
  di datetime,
  m varchar(20),
  d varchar(12),
  d_sequence_num smallint
)

insert into visit_view values ('LEE','BUDDY','','05/20/2010','10/01/2012','123456','786.2','2') 
insert into visit_view values   ('LEE','BUDDY','','05/20/2010','10/01/2012','123456','784.99','3') 
insert into visit_view values ('TU','BIBO','LU','09/29/2012','10/01/2012','321456','774.6','1') 
insert into visit_view values ('SMITH','BOBBIE','JOE','09/29/2012','10/01/2012','321654','V50.2','1')
insert into visit_view values ('LEWIS','CAREY','','11/11/2011','10/01/2012','654123','057.9','2')
insert into visit_view values ('LEWIS','CAREY','','11/11/2011','10/01/2012','654123','074.3','3')
insert into visit_view values ('RAMIREZ','HECTOR','','04/21/2011','10/02/2012','654321','381.81','2')
insert into visit_view values ('RAMIREZ','HECTOR','','04/21/2011','10/02/2012','654321','786.09','3')
insert into visit_view values ('RAMIREZ','HECTOR','','04/21/2011','10/02/2012','654321','380.4','4')
insert into visit_view values ('RAMIREZ','HECTOR','','04/21/2011','10/02/2012','654321','478.19','5')

我的查询看起来像这样......

SELECT  
    first_name AS FirstName, 
    ISNULL(middle_name, '') AS MI, 
    last_name AS LastName, 
    CONVERT(varchar,dob, 101) DOB,
    CONVERT(varchar,di,101) DCdate,
    CAST(m AS INT) AS MR,
    d AS Diag
FROM 
    visit_view 
WHERE 
    d_sequence_num>1
    AND DATEDIFF(year,dob,GETDATE()) <= 3
ORDER BY di,d_sequence_num ASC

我的输出是

FIRSTNAME MI LASTNAME DOB DCDATE MR DIAG 
CAREY  LEWIS 11/11/2011 10/01/2012 654123 057.9 
BUDDY  LEE 05/20/2010 10/01/2012 123456 786.2 
BUDDY  LEE 05/20/2010 10/01/2012 123456 784.99 
CAREY  LEWIS 11/11/2011 10/01/2012 654123 074.3 
HECTOR  RAMIREZ 04/21/2011 10/02/2012 654321 381.81 
HECTOR  RAMIREZ 04/21/2011 10/02/2012 654321 786.09 
HECTOR  RAMIREZ 04/21/2011 10/02/2012 654321 380.4 
HECTOR  RAMIREZ 04/21/2011 10/02/2012 654321 478.19 

但我想要这样......

FIRSTNAME MI LASTNAME DOB DCDATE MR DIAG 
CAREY  LEWIS 11/11/2011 10/01/2012 654123 057.9 
BUDDY  LEE 05/20/2010 10/01/2012 123456 786.2 784.99
CAREY  LEWIS 11/11/2011 10/01/2012 654123 074.3 
HECTOR  RAMIREZ 04/21/2011 10/02/2012 654321 381.81 786.09 380.4 478.19 

2 个答案:

答案 0 :(得分:1)

我不确定您为什么CAREY LEWIS在结果中显示两次,HECTOR RAMIREZ显示一次,但您可以使用以下内容合并行:

SELECT  
    first_name AS FirstName, 
    ISNULL(middle_name, '') AS MI, 
    last_name AS LastName, 
    CONVERT(varchar,dob, 101) DOB,
    CONVERT(varchar,di,101) DCdate,
    CAST(m AS INT) AS MR,
    stuff((select distinct ', '+ d 
           from visit_view v1
           where v.first_name = v1.first_name
           FOR XML PATH('')),1,1,'') Diag   
FROM visit_view v
WHERE d_sequence_num>1
    AND DATEDIFF(year,dob,GETDATE()) <= 3
GROUP BY first_name, middle_name, last_name, dob, di, m
ORDER BY di ASC

请参阅SQL Fiddle with Demo

给出结果:

| FIRSTNAME | MI | LASTNAME |        DOB |     DCDATE |     MR |                           DIAG |
-------------------------------------------------------------------------------------------------
|     BUDDY |    |      LEE | 05/20/2010 | 10/01/2012 | 123456 |                  784.99, 786.2 |
|     CAREY |    |    LEWIS | 11/11/2011 | 10/01/2012 | 654123 |                   057.9, 074.3 |
|    HECTOR |    |  RAMIREZ | 04/21/2011 | 10/02/2012 | 654321 |  380.4, 381.81, 478.19, 786.09 |

答案 1 :(得分:0)

我建议您查看CROSS APPLYFOR XML

SELECT  DISTINCT
    first_name AS FirstName, 
    ISNULL(middle_name, '') AS MI, 
    last_name AS LastName, 
    CONVERT(varchar,dob, 101) DOB,
    CONVERT(varchar,di,101) DCdate,
    CAST(m AS INT) AS MR,
     STUFF(D2.d, 1, 1, '')  AS Diag
FROM 
    visit_view v
CROSS APPLY ( SELECT ',' + d
              FROM visit_view v2
              WHERE v.last_name = v2.last_name
              ORDER BY d_sequence_num 
                  FOR XML PATH('') )  D2 ( d )
WHERE 
    d_sequence_num>1
    AND DATEDIFF(year,dob,GETDATE()) <= 3

不确定为什么你的visit_view表中没有ID,或者在我的十字架申请中使用了那个标准,但这应该让你朝着正确的方向前进。

这是SQL Fiddle

祝你好运。