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.
romsomIT DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
So there could be multiple AliasName for each DeviceName?
0
romsomIT DeveloperAuthor Commented:
Yes, there could be multiple AliasNames for each DeviceName.
DeviceName is the primary key in the DeviceInfo table.
0
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
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

NorieAnalyst Assistant Commented:
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
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NorieAnalyst Assistant Commented:
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
romsomIT DeveloperAuthor 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
romsomIT DeveloperAuthor Commented:
Thank you very much, both solutions work.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.