0

I'm new to writing queries in SQL and looking for some tips

I ran a query that resulted in the table below:

Company Name Seller Investor Date
App SellerX InvestorA 01/01/2000
App SellerX InvestorB 01/01/2000
Phone SellerY InvestorC 09/08/2000
Phone SellerY InvestorD 09/08/2000
Phone SellerY InvestorE 09/08/2000

And I would like my results to look like this:

Company Name Seller Investor Date
App SellerX InvestorA,InvestorB 01/01/2000
Phone SellerY InvestorC,InvestorD,InvestorE 09/08/2000

Anyone can help on the best way to do this?

Thanks!!

CatiS
  • 1

2 Answers2

0

You need string_agg as follows:

Select company_name, seller,
       String_agg(investor, ',') within group (order by investor) as investor,
       Date
  From (your_query) t
 Group by company_name, seller, date;
Popeye
  • 35,427
  • 4
  • 10
  • 31
0

By using STUFF

SELECT companyname,seller,STUFF((SELECT CONCAT(',' ,Investor) 
FROM table t1
WHERE t1.companyname = t2.companyname
AND t1.seller=t2.seller
FOR XML PATH ('')), 1, 1, '') AS Investor,date
From table t2
Thiyagu
  • 1,260
  • 1
  • 5
  • 14