Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql query help

Posted on 2015-01-13
3
Medium Priority
?
82 Views
Last Modified: 2015-01-22
I have a query and what I am trying to do is for the same location create a list of days
The data looks like this

locationID      availableDays
97      1
97      2
104      1
104      2
104      3
104      4
104      5

What I need is this
locationID          availableDays
97                              1,2
104                             1,2,3,4,5

SELECT 
      locationID,
      availableDays
  FROM lkup_locationByDay Ld
  CROSS APPLY (
		SELECT cast(ll.availableDays as VARCHAR) + ','
		from lkup_locationByDay LL
		where ld.ID = ll.id
		FOR XML PATH('')
  )AS cross1(AVAILlOCATIONS)
  
  where locationID in (4,5,97,104,110,111,112,211,232,226,256)
  order by locationID

Open in new window

0
Comment
Question by:erikTsomik
3 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40547156
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40547177
Bingo bango..
IF OBJECT_ID('tempdb..#tmp') IS NOT NULL
   DROP TABLE #tmp
GO

CREATE TABLE #tmp (locationID int, availableDays int)

INSERT INTO #tmp (locationID, availableDays) 
VALUES (97,1), (97,2), (104,1), (104,2), (104,3), (104,4), (104,5)

SELECT locationID, LEFT(availableDays, LEN(availableDays) -1) as details
FROM ( 
   SELECT DISTINCT t1.locationID,
      stuff((
      SELECT ' ' + CAST(availableDays as varchar(10)) + ', '
      FROM #tmp t2 
      WHERE t1.locationID = t2.locationID
      FOR XML PATH('')), 1, 1, '') as availableDays
   FROM #tmp t1) a

Open in new window

0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40547191
Your original query just needs some slight adjustments:

  SELECT
      ld.locationID,
      STUFF(cross1.AVAILlOCATIONS, 1, 1, '') AS availableDays
  FROM (
      SELECT DISTINCT locationID
      FROM lkup_locationByDay
  ) AS Ld
  CROSS APPLY (
            SELECT ',' + cast(ll.availableDays as VARCHAR)
            from lkup_locationByDay LL
            where ld.locationID = ll.locationid
            FOR XML PATH('')
  )AS cross1(AVAILlOCATIONS)
  WHERE locationID in (4,5,97,104,110,111,112,211,232,226,256)
  ORDER BY locationID
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

926 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