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

x
?
Solved

Horizontal SQL Query

Posted on 2012-03-28
3
Medium Priority
?
649 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 400 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 800 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 800 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

824 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