将数据合并到一列中

时间:2017-06-23 01:58:56

标签: sql sql-server

我使用sql server设置了这些原始数据。

Company | Address | Business            | Telephone Number | Contact Person

A&B     | Perak   | Khmer Restaurants   | 012541           | Mr. Yu Lee
A&B     | Perak   | F&B                 | 012541           | Mr. Yu Lee
King Co.| Ipoh    | Paper Distributors  | 021453           | Mrs. Cheng
King Co.| Ipoh    | Paper Distributors  | Null             | Mrs. Cheng
DinoX   | Sunway  | Guesthouses         | 0124587          | Mr. Hong
Dinox   | Sunway  | Guesthouses         | 0124587          | Mr. Q

经过一些修改,我的数据集变得像这样,下面是语法:

IF OBJECT_ID('tempdb..#tCat') IS NOT NULL DROP TABLE #tcat
GO
DECLARE @portal varchar(100) = 'A4E7890F-A188-4663-89EB-176D94DF6774'
SELECT * INTO #tcat
FROM (
SELECT DISTINCT  list.[name]
                ,lc.listing_uuid
                ,dCat.[name]  as Category
                ,catg.[name]  as Sub_Category
                ,comm.[value] as Telephone_Number
                ,pp.title + ' ' + pp.first_name + ' ' + pp.last_name as Contact_Person
FROM panpages.listings as list
LEFT JOIN panpages.listing_categories as lc on lc.listing_uuid=list.uuid AND lc.portal_uuid=@portal
LEFT JOIN panpages.categories as catg on catg.uuid=lc.category_uuid AND catg.portal_uuid=@portal
left join panpages.listing_people as lp on lp.listing_uuid = list.uuid
left join panpages.people as pp on pp.id = lp.person_id
left join panpages.person_communications as comm on comm.person_id = lp.person_id
LEFT JOIN ( SELECT DISTINCT uuid,[name] FROM panpages.categories WHERE parent_uuid IS NULL ) as dCat on dCat.uuid=catg.parent_uuid
WHERE list.portal_uuid=@portal and list.is_active=1
)as tCat

select
     list.[name] as [Company]
    ,list.[address] as [Address]
    ,replace(cats.Sub_Category,'&','&')     as [Nature of Business]
    ,replace(cats.Telephone_Number,'&','&') as [Telephone Number]
    ,replace(cats.Contact_Person,'&','&')   as [Contact Person]

from [panpages].[listings] as list
left join ( 
            SELECT DISTINCT tc1.listing_uuid,tc1.[name],
            Sub_Category = STUFF(( SELECT   ',' + tc2.Sub_Category
                 FROM #tCat as tc2 
                 WHERE tc1.listing_uuid = tc2.listing_uuid  
                 ORDER BY tc2.Sub_Category 
                 FOR XML PATH('')), 1, 1, ''),
            Telephone_Number = STUFF(( SELECT   ',' + tc2.Telephone_Number
                 FROM #tCat as tc2 
                 WHERE tc1.listing_uuid = tc2.listing_uuid  
                 ORDER BY tc2.Telephone_Number 
                 FOR XML PATH('')), 1, 1, ''),
            Contact_Person = STUFF(( SELECT ',' + tc2.Contact_Person
                 FROM #tCat as tc2 
                 WHERE tc1.listing_uuid = tc2.listing_uuid  
                 ORDER BY tc2.Contact_Person
                 FOR XML PATH('')), 1, 1, '')
            FROM #tCat as tc1 where tc1.listing_uuid is not null
        ) cats on cats.listing_uuid=list.uuid
where
    list.[portal_uuid]=@portal and
    list.[is_active]=1

输出:

Company | Address | Business                                 | Telephone Number | Contact Person

A&B     | Perak   | Khmer Restaurants,F&B                    | 012541,012541    | Mr. Yu Lee,Mr. Yu Lee
King Co.| Ipoh    | Paper Distributors, Paper Distributors   | 021453,Null      | Mrs. Cheng,Mrs. Cheng
DinoX   | Sunway  | Guesthouses,Guesthouses                  | 0124587,0124587  | Mr. Hong,Mr. Q

但是,我的预期结果是这样的:

Company |Address| Business              | Telephone Number | Contact Person

A&B     | Perak | Khmer Restaurants, F&B| 012541           | Mr. Yu Lee
King Co.| Ipoh  | Paper Distributors    | 021453, Null     | Mrs. Cheng
DinoX   | Sunway| Guesthouses           | 0124587          | Mr. Hong, Mr Q

有人有任何想法吗?真的需要你的帮助。

0 个答案:

没有答案