Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join columns SQL query

I have three tables : enter image description here
For example here is data on database :

enter image description here
Is it possible to write query that provide a grid like below structure?

enter image description here
With writing query using simple join the result is like here :

SELECT     dbo.Contact.ContactID, dbo.Contact.ContactName, dbo.PhoneNumber.PhoneNO, dbo.PhoneType.TypeTitle
FROM         dbo.Contact INNER JOIN
                      dbo.PhoneNumber ON dbo.Contact.ContactID = dbo.PhoneNumber.ContactID AND dbo.Contact.ContactID = dbo.PhoneNumber.ContactID INNER JOIN
                      dbo.PhoneType ON dbo.PhoneNumber.PhoneType = dbo.PhoneType.PhoneTypeI

enter image description here

like image 928
Shahin Avatar asked Sep 03 '11 08:09

Shahin


1 Answers

What you are looking for is string aggregation. T-SQL doesn't natively do it (other dbs have string_agg for example). BUT you can simulate it.

Try looking for example: http://consultingblogs.emc.com/jamiethomson/archive/2009/07/16/string-aggregation-in-t-sql-amp-pl-sql.aspx

or, for the completists:

http://www.postgresonline.com/journal/archives/191-stringagg.html

if you search for SQL Server in the last link, there are three different ways to do it.

like image 120
xanatos Avatar answered Nov 02 '22 10:11

xanatos