Solved

SQL - comma seperated values

Posted on 2013-07-01
4
396 Views
Last Modified: 2013-07-01
Hello experts,

Here is my current SQl and output:

SELECT HU.HubUnitId , HUR.PostalCode
  FROM [Ecommerce].[dbo].[HubUnit] HU
  Join [Ecommerce].[dbo].[HubUnitRegion] HUR
  ON  HU.HubUnitId = HUR.HubUnitId

HubUnitId  PostalCode
3003      46301
3003      46302
3003      46303
3003      46304
3003      46307
3003      61254
3003      61256
3004      76649
3004      76650
3004      76651
3004      76652
3004      76653
3004      76654
3004      76655
3004      76656
3004      76657
3004      76660


I want to write a sql that will generate values like shown below:

HubUnitID   PostalCode
3003             46301, 46302, 46303, 46304
3004             76200,76201,76202,76203


any idea?

Thanks
0
Comment
Question by:mani_sai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 16

Accepted Solution

by:
EvilPostIt earned 500 total points
ID: 39290079
create table #mytable (hub int,post int)
insert into #mytable values(3003,46301)
insert into #mytable values(3003,46302)
insert into #mytable values(3003,46303)
insert into #mytable values(3003,46304)
insert into #mytable values(3003,46307)
insert into #mytable values(3003,61254)
insert into #mytable values(3003,61256)
insert into #mytable values(3004,76649)
insert into #mytable values(3004,76650)
insert into #mytable values(3004,76651)
insert into #mytable values(3004,76652)
insert into #mytable values(3004,76653)
insert into #mytable values(3004,76654)
insert into #mytable values(3004,76655)
insert into #mytable values(3004,76656)
insert into #mytable values(3004,76657)
insert into #mytable values(3004,76660)

SELECT 
  [Hub],
  STUFF((
    SELECT CAST([post] AS VARCHAR(MAX))+','
    FROM #mytable
    WHERE (hub = Results.hub) 
    FOR XML PATH (''))
  ,1,0,'') AS NameValues
FROM #mytable Results
GROUP BY hub

Open in new window

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39290109
0
 
LVL 20

Expert Comment

by:dsacker
ID: 39290132
DECLARE @HubUnit TABLE (
    HubUnitID   smallint    NOT NULL,
    PostalCode  varchar(5)  NOT NULL )

INSERT INTO @HubUnit
SELECT 3003, '46301' UNION ALL
SELECT 3003, '46302' UNION ALL
SELECT 3003, '46303' UNION ALL
SELECT 3003, '46304' UNION ALL
SELECT 3003, '46307' UNION ALL
SELECT 3003, '61254' UNION ALL
SELECT 3003, '61256' UNION ALL
SELECT 3004, '76649' UNION ALL
SELECT 3004, '76650' UNION ALL
SELECT 3004, '76651' UNION ALL
SELECT 3004, '76652' UNION ALL
SELECT 3004, '76653' UNION ALL
SELECT 3004, '76654' UNION ALL
SELECT 3004, '76655' UNION ALL
SELECT 3004, '76656' UNION ALL
SELECT 3004, '76657' UNION ALL
SELECT 3004, '76660'

SELECT  DISTINCT HubUnitID,
        SUBSTRING((SELECT ',' + PostalCode FROM @HubUnit h2
                   WHERE  h2.HubUnitID = h1.HubUnitID
                   FOR XML PATH('')), 2, 999999) AS PostalCode
FROM    @HubUnit h1

Open in new window

0
 
LVL 8

Author Closing Comment

by:mani_sai
ID: 39290151
It worked. thanks guys.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sql query with where clause 2 45
TSQL Challenge... 7 45
GeoClustering  and AOG 25 43
Cannot connect to SQL express 2008 R2 installed on Server 2016 5 21
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question