Solved

Horizontal SQL Query

Posted on 2012-03-28
3
625 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

751 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