Solved

sql query help

Posted on 2015-01-13
3
69 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 69

Expert Comment

by:Éric Moreau
ID: 40547156
0
 
LVL 65

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 69

Accepted Solution

by:
ScottPletcher earned 500 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now