2

As you can see I have 4 table to join below and I've search for the stuff function but it seems to support a single table only.

DECLARE @portal varchar(100) = 'A4E7890F-A188-4663-89EB-176D94DF6774'
select 
     list.[name]     as [Company]
    ,list.[address]  as [Address]
    ,b.[name]        as [Business]
    ,comm.[value]    as [Telephone Number]
    ,pp.title + ' ' + pp.first_name + ' ' + pp.last_name as [Contact Person]

from [panpages].[listings] as list
    left join panpages.extra_communications as comm
        on comm.listing_uuid = list.uuid and comm.type = 'Tel'
    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.listing_categories as r
        on r.listing_uuid = list.uuid
    left join panpages.categories as b
        on b.uuid = r.category_uuid
where
    list.[portal_uuid]=@portal and
    list.[is_active]=1 

This is my sql code and the data shown like below.

My current result appear like this :

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

My expected result should be like this :

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

Can somebody help me? I'm stuck around 4 hours because of this shit. Thanks in advance.

Iqmal
  • 55
  • 7
  • Are you using SQL Server? Or Access? – Tim Biegeleisen Jun 22 '17 at 02:54
  • Hi Tim, i'm using SQL Server. – Iqmal Jun 22 '17 at 03:00
  • Hi Tim, i'm afraid that forum that you marked is using a single table only.. My problem is i have 4 table to join and it seems like all the example i found is only use a single table. – Iqmal Jun 22 '17 at 03:22
  • The number of tables in the join should have nothing to do with it. – Tim Biegeleisen Jun 22 '17 at 03:48
  • Can you at least give me a syntax based on my current syntax? Where should i add the XML path and how? I really have no idea about it. – Iqmal Jun 22 '17 at 03:54
  • I partially recant what I said. Your requirement to have only distinct businesses in your output is tricky. – Tim Biegeleisen Jun 22 '17 at 04:08
  • Yes it's tricky and i'm new in this field. Really don't know what to do. Has been trying for 6 hours without success. By the way, thanks for your time Tim. – Iqmal Jun 22 '17 at 04:13
  • Hi OMG, thanks for the suggestion. I really have no idea what is XML syntax is about. Have been trying without success. Can you give me a syntax based on my current syntax? It would be really helpful. – Iqmal Jun 22 '17 at 04:33
  • @Iqmal Did you get your desired result –  Jun 22 '17 at 08:26
  • yeah i got it but new problem appear. lol – Iqmal Jun 23 '17 at 01:46

0 Answers0