Solved

Horizontal SQL Query

Posted on 2012-03-28
3
598 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

771 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

16 Experts available now in Live!

Get 1:1 Help Now