2013-01-22 96 views
0

我有一個表中有一列中具有唯一值的行。我怎樣才能把這些放在一行中,使一個條目有多個條目?如何將重複列轉換爲行?

例如這裏是一個表...

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 
+0

嘗試:http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-microsoft-sql-server-2005 –

+0

並在這裏爲SQL Server解決方案:http://support.microsoft.com/kb/2046037/sql-server-can-i-comma-delimit-multiple-rows-into-one-column – Art

回答

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 | 
+0

凱里兩次是我的錯誤,這正是我所期待的! – dwtorres

+0

+1爲SQL小提琴 - 我不知道存在 - 它已被添加到我的書籤欄 –

+0

@dwtorres高興地幫助,如果他們有幫助,一定要接受你的問題的答案之一。您可以通過點擊答案左側的複選標記來接受 – Taryn

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 

不知道爲什麼你沒有一個ID在你visit_view表或會用在我的交叉應用,其中的標準,但這應該讓你朝着正確的方向前進。

這裏是SQL Fiddle

祝你好運。