Solved

Horizontal SQL Query

Posted on 2012-03-28
3
631 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

617 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