473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SQL Server 2005: How to turn multiple rows into a single column

How do I display multiple rows in a single column within the SQL Server 2005?

Sample Data

-----------------------------------------------------
Cust Name Cust_Order
------------------------------------------------------
ABCD ------ Calculator
ABCD ------ Radio
ABCD ------ TV
EFGH ------ Radio
IJKL ------ TV

Sample Result

-----------------------------------------------------
Cust Name Cust_Order
------------------------------------------------------
ABCD ------ Calculator, Radio, TV
EFGH ------ Radio
IJKL ------ TV
Dec 13 '07 #1
2 2976
jamesd0142
469 256MB
This can be modified to suit i think...

Expand|Select|Wrap|Line Numbers
  1. --results on 1 row........................
  2. DECLARE @a varchar(8000)
  3. SELECT @a=COALESCE(@a, '') + col01 + space(1) FROM (SELECT distinct (col01) from Dataset_Rows) as a
  4. SELECT rtrim(ltrim(@a)) AS [MyValues]
  5.  
Dec 13 '07 #2
Jim Doherty
897 Expert 512MB
This can be modified to suit i think...

Expand|Select|Wrap|Line Numbers
  1. --results on 1 row........................
  2. DECLARE @a varchar(8000)
  3. SELECT @a=COALESCE(@a, '') + col01 + space(1) FROM (SELECT distinct (col01) from Dataset_Rows) as a
  4. SELECT rtrim(ltrim(@a)) AS [MyValues]
  5.  

Hi James and eaocomp

The modification required to make this work for the poster centres around the fact that a concatenated list of values is required for each of the Customer names given. The code snippet you highlighted for amendment is not CustName centric (unless of course Dataset_Rows has some restrictive criteria based on the CustName) therefore it would return everything if you get me.

In short, for each customer the SQL is required to trundle down the products column as it were, and grab each unique mention of a product and fetch those values into 'one line' per customer separating out the products with a delimiter value which in this case is a required comma.

If the user creates a 'user defined function' to perform this functionality then this can be referenced in any views with the 'required' parameter being passed from the 'CustName' column value and the defined line of products return values then become visible in the view as a displayed column. We know that we can reference views in any stored procedures so we get the best of both worlds should they ever be required later.

This is the sample SQL that demonstrates the point of calling the UDF(User defined function) the udf function call being bolded

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT DISTINCT [Cust Name], dbo.UDF_CustOrder([Cust Name]) AS Cust_Order
  3. FROM dbo.[Sample Data]
  4.  

The user defined function below to make this work is based on the table name 'Sample Data' as given obviously amend that to suit (hate spaces in object names but there you go) as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE FUNCTION dbo.UDF_CustOrder
  3. ( @custname varchar (50) )
  4. RETURNS varchar(8000)
  5. AS
  6. BEGIN
  7. DECLARE @Custorder varchar(255), @delimiter char
  8. SET @delimiter = ','
  9. SELECT @custorder = COALESCE(@custorder + @delimiter, '') +space(1)+ Cust_Order FROM (SELECT DISTINCT [Cust Name],Cust_Order FROM [Sample Data] where [Cust Name]=@CustName) c
  10. RETURN ( SELECT ltrim(rtrim(@CustOrder)) AS [CustOrder])
  11. END
  12.  

Hope this helps

Jim :)
Dec 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: JB | last post by:
To anyone that is able to help.... What I am trying to do is this. I have two tables (Orders, and OrderDetails), and my question is on the order details. I would like to set up a stored...
16
by: bika | last post by:
I have a table that has values as follows: PersonID Degree 55 MD 55 Phd 55 RN 60 MD 60 Phd I need a create a query that will give me output like this:
3
by: Asaf | last post by:
Hi, I have a MSSQL 2005 test DB with two tables: Table "T1Customers": T1CustomersRowEnum (PK, int, Not Null) T1CustomersFullName (nvarchar(50) null) Table "T2Details":
7
by: Mintyman | last post by:
Hi, I'm working on a system migration and I need to combine data from multiple rows (with the same ID) into one comma separated string. This is how the data is at the moment: Company_ID ...
3
Atli
by: Atli | last post by:
Hi. I've been trying to insert multiple rows into a table using a single INSERT statement in MSSQL / SQL Server 2005. I could of course cheat and have my C# code insert each row using some sort...
0
Coldfire
by: Coldfire | last post by:
Since i cannot show the differences in a two-column like table. I am first putting MS SQL Server 2005 and then MySQL 5.x. MS SQL Server 2005 Brief Overview - SQL Server is a full-fledged...
1
by: gaholmes | last post by:
Hi, I am stumped and was hoping someone could help me out. Any help is appreciated. I have a view that looks sort of like this (but with a lot more entries of course) ...
6
chiragvithlani
by: chiragvithlani | last post by:
In SQL Server 2005... with some 11K rows I am having two tables T1 and T2(as bkup of T1), now i need one column data of T2 as i accidently updated T1 column data, (Update T1.colx with T2.colx) ...
2
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.