MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 9th, 2012, 02:50 PM
Ju1iaWhite Ju1iaWhite is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 1 Ju1iaWhite User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m
Reputation Power: 0
Question Combine field data from several rows into one row

I've seen several posts for this type of issue - but have not seen any solutions.

I am a report developer (new to SQL Svr). I am using SQL Server 2008 r2. I have three tables. The main table is the Provider table. It connects to the Prov_Target_Pops table on the Provider table primary key. The Prov_Target_Pops table connects to the Target_Population table which lists the descriptions of the target pops.

My result set displays a row for each provider for each target pop. I want the result set to show one row per provider with the target pops combined (separated by commas) in one field.

My query is:
SELECT DISTINCT
tbl_Providers.PROV_ID, tbl_Providers.PROV_NAME, tbl_Providers.ADDRESS1, tbl_Providers.ADDRESS2, tbl_Providers.CITY, tbl_Providers.STATE,
tbl_Providers.ZIP_CODE, tbl_pp_provider.phys_phone, tbl_Providers.COUNTY, tbl_Target_Population.DESCRIPTION AS PopSvd

FROM tbl_Providers INNER JOIN
tbl_Prov_Target_Pops ON tbl_Providers.PROV_ID =
tbl_Prov_Target_Pops.PROV_ID INNER JOIN
tbl_Target_Population ON
tbl_Prov_Target_Pops.TARGET_POP_ID =
tbl_Target_Population.TARGET_POP_ID INNER JOIN
tbl_pp_provider ON tbl_Providers.PROV_ID =
tbl_pp_provider.prov_id

GROUP BY tbl_Providers.PROV_ID, tbl_Providers.PROV_NAME,
tbl_Providers.ADDRESS1, tbl_Providers.ADDRESS2,
tbl_Providers.STATE, tbl_Providers.ZIP_CODE,
tbl_Providers.COUNTY,
tbl_Target_Population.DESCRIPTION, tbl_Providers.CITY,
tbl_pp_provider.phys_phone

HAVING (tbl_Providers.COUNTY IN
('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S'))

ORDER BY tbl_Providers.COUNTY, tbl_Providers.CITY,
tbl_Providers.PROV_NAME

The sample result set is:

Prov_ID PROV_NAME Description
1 Provider 1 Svc 1
1 Provider 1 Svc 2
1 Provider 1 Svc 3
1 Provider 1 Svc 4
1 Provider 1 Svc 5
1 Provider 1 Svc 6
3 Provider 3 Svc 1
3 Provider 3 Svc 2
3 Provider 3 Svc 3
3 Provider 3 Svc 4

The desired result set is:

Prov_ID PROV_NAME Description
1 Provider 1 Svc 1, Svc 2, Svc 3, Svc 4,
Svc 5, Svc 6
3 Provider 3 Svc 1, Svc 2, Svc 3, Svc 4

Can someone please help me with this?

Reply With Quote
  #2  
Old April 9th, 2012, 05:20 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 54 m 33 sec
Reputation Power: 4140
Quote:
Originally Posted by Ju1iaWhite
I've seen several posts for this type of issue - but have not seen any solutions.
that's because there really aren't any

i think there is one using XML but it's really clunky and i don't know the syntax offhand

why can't you do the concatenation in your application layer?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Combine field data from several rows into one row

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap