SQL Syntax

Hi Experts,
Please take a look at the query below:

SELECT i.DeviceName, AliasName FROM DeviceInfo i LEFT OUTER JOIN DeviceAliasMapping a ON i.DeviceName = a.DeviceName order by i.DeviceName

DeviceName is returned many times, because in the DeviceAliasMapping table the same DeviceName has many AliasNames

I would like the DeviceName returned only once, and the AliasNames separated by commas in the AliasName column

Can you please let me know how to do it? I appreciate your help.
romsomAsked:
Who is Participating?
 
Ryan ChongCommented:
DeviceName is the primary key in the DeviceInfo table.
just wondering why Device's Name (chars) are defined as the primary key. if this value is changed often then it would be not suitable to be used as a primary key.

anyway, and i think Norie's solution can be simplified to:

SELECT i.DeviceName, (SELECT DISTINCT a.AliasName+',' FROM DeviceAliasMapping a where i.DeviceName = a.DeviceName  FOR XML path('')) AS Aliases
FROM DeviceInfo i
GROUP by i.DeviceName

Open in new window


but with extra "," at the back of Aliases.

OR you can use something like below with Stuff function (in MS SQL):

declare @DeviceInfo table
(
	DeviceName varchar(100)
)

insert into @DeviceInfo
values
('ABC'),
('DEF'),
('KKK');

declare @DeviceAliasMapping table
(
	DeviceName varchar(100),
	AliasName varchar(100)
)

insert into @DeviceAliasMapping
values
('ABC', 'Alias 1'),
('ABC', 'Alias 2'),
('ABC', 'Alias 3'),
('DEF', 'vvx'),
('KKK', 'dsd'),
('KKK', 'dsd 2sx');

SELECT i.DeviceName,
	STUFF((SELECT ', ' + a.AliasName
		FROM @DeviceAliasMapping a
		WHERE i.DeviceName = a.DeviceName
		FOR XML PATH('') 
	), 1, 1, '' ) AliasName
FROM @DeviceInfo i
order by i.DeviceName

Open in new window

0
 
NorieVBA ExpertCommented:
So there could be multiple AliasName for each DeviceName?
0
 
romsomAuthor Commented:
Yes, there could be multiple AliasNames for each DeviceName.
DeviceName is the primary key in the DeviceInfo table.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Jan LouwerensSoftware EngineerCommented:
You didn't sway which database you're using, but in Oracle, you can do it like this:

SELECT DeviceName, SUBSTR(SYS_CONNECT_BY_PATH(AliasName, ','), 2) AliasNames FROM
(
   SELECT i.DeviceName, AliasName, ROW_NUMBER() OVER (PARTITION BY i.DeviceName ORDER BY AliasName) RowNumber FROM DeviceInfo i
   LEFT OUTER JOIN DeviceAliasMapping a ON
      i.DeviceName = a.DeviceName
)
START WITH
   RowNumber = 1
CONNECT BY
   PRIOR DeviceName = DeviceName AND
   PRIOR RowNumber + 1 = RowNumber
ORDER SIBLINGS BY AliasName

Open in new window

0
 
NorieVBA ExpertCommented:
This should work but I'm sure there's a better way to do it.

SELECT i.DeviceName, (SELECT DISTINCT a.AliasName+',' FROM DeviceAliasMapping LEFT OUTER JOIN DeviceAliasMapping a ON i.DeviceName = a.DeviceName  FOR XML path('')) AS Aliases
FROM DeviceInfo i LEFT OUTER JOIN DeviceAliasMapping a ON i.DeviceName = a.DeviceName
GROUP by i.DeviceName

PS That's for SQL Server.
0
 
Jan LouwerensSoftware EngineerCommented:
Or, simpler for Oracle:

SELECT i.DeviceName, LISTAGG(AliasName, ', ') WITHIN GROUP (ORDER BY Topic_Reference.Topic_ID) AliasNames FROM DeviceInfo i
LEFT OUTER JOIN DeviceAliasMapping a ON
   i.DeviceName = a.DeviceName
GROUP BY i.DeviceName

Open in new window

0
 
NorieVBA ExpertCommented:
Ryan

I could have sworn I tried that simplification of what I posted but it seemed to be duplicating things, must have got something wrong somewhere.:)
0
 
romsomAuthor Commented:
I'm sorry I didn't mention, but I'm using SQL Server database. Thank you for all your replies. I'm trying to make it work now.
0
 
romsomAuthor Commented:
Thank you very much, both solutions work.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.