Solved

Horizontal SQL Query

Posted on 2012-03-28
3
605 Views
Last Modified: 2012-08-13
I have a table with the following rows and I need to produce a view that returns 1 row per customer with the 'TaxDetail' values comma-separated.

Example Values:

CustomerID, | TaxDetailID (this is the column header)
1 | 01
1 | 02
1 | 03
2 | 01
2 | 02

I need a query that would return the 2 rows below,

1 | 01, 02, 03
2 | 01, 02

Any advice is appreciated.

Dean
0
Comment
Question by:dthansen
3 Comments
 
LVL 18

Assisted Solution

by:lludden
lludden earned 100 total points
ID: 37777234
DECLARE @T TABLE (CustomerID int, TaxDetailID varchar(10))

INSERT INTO @T SELECT 1,'01' UNION SELECT 1,'02' UNION SELECT 1,'03' UNION SELECT 2,'01' UNION SELECT 2,'02'

SELECT DISTINCT T.CustomerID,
IDList = Stuff((SELECT DISTINCT  ', ' + T1.TaxDetailID

                            FROM @t T1

                            WHERE t.CustomerID = T1.CustomerID

                            FOR XML PATH ('')),1,1,'') 	
FROM @T T

Open in new window

0
 

Accepted Solution

by:
lmduffy earned 200 total points
ID: 37777242
I use a UDF() for that type of thing. Basically you write an SQL function that takes the CustomerID as parameter. The function would then look up the TaxDetailIDs for that customer and return them all in a string formatted however you like. In this way, you can do something like this in your main code:

SELECT Distinct CustomerID,
       GetTaxDetails( CustomerID ) as TaxIDs
FROM MyTable

Open in new window

0
 
LVL 7

Assisted Solution

by:micropc1
micropc1 earned 200 total points
ID: 37777409
I 'm with Imduffy...UDF's the way to go here...

CREATE FUNCTION getTaxDetails (@customerID int)
RETURNS varchar(max)
BEGIN
   DECLARE @list varchar(10)
   SELECT @list = ISNULL(@list, '') + TaxDetailID + ', '
   FROM MyTable
   WHERE CustomerID = @customerID
   RETURN @list
END

Open in new window


then do as mentioned above...

SELECT customerID, dbo.getTaxDetails(customerID)  AS TaxIDs
FROM MyTable

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard 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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

895 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

19 Experts available now in Live!

Get 1:1 Help Now