我有如下所示的数据集;
公司|地址|业务|电话号码|联系人
a
在进行如下所示的查询后,我得到一个新的数据集:
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 and (comm.communication_type = 'Mobile Phone' or comm.communication_type = 'Tel')
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
(
SELECT Sub_Category, MIN(listing_uuid) AS listing_uuid
FROM #tCat
GROUP BY Sub_Category
) 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
(
SELECT Telephone_Number, MAX(listing_uuid) AS listing_uuid
FROM #tCat
GROUP BY Telephone_Number
) 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
(
SELECT Contact_Person, MAX(listing_uuid) AS listing_uuid
FROM #tCat
GROUP BY Contact_Person
) 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
如何不返回空值?我不想返回“Null”值。
答案 0 :(得分:0)
您可以在检查之前使用ISNULL功能,也可以使用COALESCE功能,如下所示
SELECT COALESCE(Telephone_Number,''), MAX(listing_uuid) AS listing_uuid
替换你要在'内返回的任何内容。 &#39 ;.如果您只想要一个空字符串,请将其保留在''
答案 1 :(得分:0)
在替换中使用STUFF来替换空值。
Telephone_Number = Replace(STUFF( SELECT ', ' + tc2.Telephone_Number
FROM
(
SELECT Telephone_Number, MAX(listing_uuid) AS listing_uuid
FROM #tCat
GROUP BY Telephone_Number
) AS tc2
WHERE tc1.listing_uuid = tc2.listing_uuid
ORDER BY tc2.Telephone_Number
FOR XML PATH('')), 1, 1, ''), ' null', ''),