3

I wanted to get distinct country values for each category.

Say for example.

A Category has many companies which in turn has many sub-compoanies under it that belong to different counties. Let's say a Cars Category and its companies are Toyota and Honda. And we all know Toyota and Honda are global companies themselves that have branches across the globe.

I wanted to display the unique values of these countries seperated by " | " using string aggr.

How do I go about doing that?

Sample Data:

Category table

------------------
ID   |   Name    |  
------------------
1    |  Cars     |    
2    |  Game     |
------------------
Company Table

---------------------------------------
ID   |  CompanyName    | Category
---------------------------------------
1    |  Toyota         |   Cars
2    |  Honda          |   Cars
3    |  Sony           |   Game
4    |  EA             |   Game
--------------------------------------
Branches table

--------------------------------------------------------------------
BranchID      |    BranchName     |    Country      | ParentCompany|
--------------------------------------------------------------------
1             | Toyota_America    |    USA          | Toyota       |
2             | Toyota_Japan      |    JP           | Toyota       |
3             | Honda_India_Mumbai|    IN           | Honda        |
4             | Honda_India_Delhi |    IN           | Honda        |
5             | Sony_Japan_Tokyo  |    JP           | Sony         |
6             | Sony_Japan_Hokaido|    JP           | Sony         |
7             | Sony_Spain        |    ES           | Sony         |
--------------------------------------------------------------------

With the example aboev I wanted the output to be:

---------------------------------------------
Category   |    Company       |   Contries  |
---------------------------------------------
Car        |  Toyota|Honda    |  USA | JP | IN     
Game       |   Sony |EA       |    JP | ES  

As you can see in the Sony example, although Japan occured twice, it was only displayed once.

I've tried using the DISTINCT with the joins and string_aggr function but it just doesn't seem to work

My Query:

SELECT DISTINCT ca.Category, co.Company, string_agg(br.Country, '|') as Countries 
FROM Category ca
LEFT JOIN Company co ON co.Category = ca.Name
LEFT JOIN Branches br ON br.ParentCompany = ca.CompanyName
Group By
ca.Category, co.Company

Now I know that linking them using ID's and whatnot is the best practice but I only made it so, it would be much more understandable.

What am I doing wrong?

2 Answers 2

3

your are missing group by(), since you are using aggregate function string_agg()

SELECT ca.Category, string_agg(co.Company, '|'), string_agg(br.Country, '|') as Countries 
FROM Category ca
LEFT JOIN Company co ON co.Category = ca.Name
LEFT JOIN Branches br ON br.ParentCompany = ca.CompanyName
GROUP BY ca.Category
1
  • This is the correct answer for the initial question. However I revised it to grouping it according to the Category
    – Gashio Lee
    Sep 12, 2019 at 8:52
0

In MSSQL Server, in order to concatenate you may use either + sign or CONCAT function. So, in your case you may change the query to something like:

SELECT DISTINCT ca.Category, co.Company, CONCAT(br.Country, N'|') as Countries 
FROM Category ca
LEFT JOIN Company co ON co.Category = ca.Name
LEFT JOIN Branches br ON br.ParentCompany = ca.CompanyName
GROUP BY ca.Category, co.Company
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.