我有一个表,其中一列中具有唯一值的行。我怎样才能将这些放在一行中,以便一个条目有多个条目?
例如这里是一个表...
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