473,408 Members | 2,839 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,408 software developers and data experts.

How to merge multi rows to single row?

Hi,

I want to merge data from multi rows to one row.
For example,
I have two table one is 'ORDER' and one is 'ORDERDETAIL'.
What I want to do is when I want to get orders against 'user'...
I want to get 'ORDER' table INNER JOINED with 'ORDERDETAIL' but with TOTAL added and merged in a sigle row instead of multiple rows.
I want to generate this function in Stored Procedure in SQL 2005.
Can you help me about this?



1. ORDERDETAIL Table
OrderID......Price.....Quantity
A..................10............1
A..................14............2
B..................15............3
C..................20............4
C..................23............5
C..................24............6

2. RESULT TABLE (Output)
OrderID.........Total
A..................(10 * 1) + (14 * 2)
B..................(15 * 3)
C..................(20*4) + (23*5) + (24*6)
Sep 24 '07 #1
7 23822
azimmer
200 Expert 100+
Hi,

I want to merge data from multi rows to one row.
For example,
I have two table one is 'ORDER' and one is 'ORDERDETAIL'.
What I want to do is when I want to get orders against 'user'...
I want to get 'ORDER' table INNER JOINED with 'ORDERDETAIL' but with TOTAL added and merged in a sigle row instead of multiple rows.
I want to generate this function in Stored Procedure in SQL 2005.
Can you help me about this?



1. ORDERDETAIL Table
OrderID......Price.....Quantity
A..................10............1
A..................14............2
B..................15............3
C..................20............4
C..................23............5
C..................24............6

2. RESULT TABLE (Output)
OrderID.........Total
A..................(10 * 1) + (14 * 2)
B..................(15 * 3)
C..................(20*4) + (23*5) + (24*6)
Expand|Select|Wrap|Line Numbers
  1. SELECT o.OrderID, sum(d.Price*d.Quantity)
  2. FROM Order o INNER JOIN OrderDetail d ON o.OrderId=d.OrderId
  3. GROUP BY o.OrderID
  4.  
Sep 24 '07 #2
Jim Doherty
897 Expert 512MB
Hi,

I want to merge data from multi rows to one row.
For example,
I have two table one is 'ORDER' and one is 'ORDERDETAIL'.
What I want to do is when I want to get orders against 'user'...
I want to get 'ORDER' table INNER JOINED with 'ORDERDETAIL' but with TOTAL added and merged in a sigle row instead of multiple rows.
I want to generate this function in Stored Procedure in SQL 2005.
Can you help me about this?



1. ORDERDETAIL Table
OrderID......Price.....Quantity
A..................10............1
A..................14............2
B..................15............3
C..................20............4
C..................23............5
C..................24............6

2. RESULT TABLE (Output)
OrderID.........Total
A..................(10 * 1) + (14 * 2)
B..................(15 * 3)
C..................(20*4) + (23*5) + (24*6)


Your output column is confusing me in that I'm not sure whether you want to see that display 'literally' or whether you merely want it summing. To sum it is relatively easy but to display it 'literally' as you have it requires extra so I am going to assume the latter

Firstly Create this function

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE FUNCTION dbo.UDF_OrderDetail
  3. ( @OrderID char(1) )
  4. RETURNS varchar(1000)
  5. AS
  6. BEGIN
  7. DECLARE @OrderDetail varchar(1000), @Delimiter char
  8. SET @Delimiter = '+'
  9. SELECT @OrderDetail = COALESCE(@OrderDetail + @Delimiter, '') + strPQ FROM 
  10. (SELECT     OrderID, Price, Quantity, '(' + LTRIM(STR(Price)) + ' * ' + LTRIM(STR(Quantity)) + ')' AS strPQ
  11. FROM         dbo.OrderDetail where OrderID=@OrderID) derived
  12. WHERE OrderID=@OrderID
  13. RETURN ( SELECT REPLACE(@OrderDetail,')+(',') + (') AS [Order_Detail])
  14. END
  15.  

Then to use it in a query or view call it like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT DISTINCT OrderID, dbo.UDF_OrderDetail(OrderID) AS Total
  3. FROM         dbo.OrderDetail
  4.  
it gives you the two column layout (OrderID and Total) output you required using only the order details table

Not sure if this helps you or not as summing seems the obvious but I can see a case where you 'might' want to see the breakdown in a continuous line like that sooooo

Regards

Jim
Sep 24 '07 #3
Expand|Select|Wrap|Line Numbers
  1. SELECT o.OrderID, sum(d.Price*d.Quantity)
  2. FROM Order o INNER JOIN OrderDetail d ON o.OrderId=d.OrderId
  3. GROUP BY o.OrderID
  4.  
Above query works well but I am facing some other problems.

I have ProductName, ProductPrice, ProductSize columns as well in OrderDetail table. I also want these columns too but when I select these columns, it gives an error of GROUP BY clause of each newely selected column.

Is there any solution. Following is the sample solution that I want

OrderID----------Total----------ProductName----------ProductPrice----------ProductSize
A-------------(25*2)+(10+1)------------ABC-------------------$10-----------------------Large
B-------------(10*1)+(1*2)+(2*1)------DEF--------------------$5------------------------Small


I hope, above result sample clears the requirement.

Thanx in advance
Sep 25 '07 #4
Expand|Select|Wrap|Line Numbers
  1. SELECT o.OrderID, sum(d.Price*d.Quantity)
  2. FROM Order o INNER JOIN OrderDetail d ON o.OrderId=d.OrderId
  3. GROUP BY o.OrderID
  4.  

Above query works well but I am facing some other problems.

I have ProductName, ProductPrice, ProductSize columns as well in OrderDetail table and Discount in Order table. I also want these columns too but when I select these columns, it gives an error of GROUP BY clause of each newely selected column.

Is there any solution. Following is the sample solution that I want

OrderID---------Total----------------ProductName---------ProductPrice---------ProductSize
A------(25*2)+(10+1)-Discount------------ABC-------------------$10---------------------------Large
B------(10*1)+(1*2)+(2*1)-Discount------DEF-------------------$5----------------------------Small


I hope, above result sample clears the requirement.

Thanx in advance
Sep 25 '07 #5
azimmer
200 Expert 100+
Above query works well but I am facing some other problems.

I have ProductName, ProductPrice, ProductSize columns as well in OrderDetail table and Discount in Order table. I also want these columns too but when I select these columns, it gives an error of GROUP BY clause of each newely selected column.

Is there any solution. Following is the sample solution that I want

OrderID---------Total----------------ProductName---------ProductPrice---------ProductSize
A------(25*2)+(10+1)-Discount------------ABC-------------------$10---------------------------Large
B------(10*1)+(1*2)+(2*1)-Discount------DEF-------------------$5----------------------------Small


I hope, above result sample clears the requirement.

Thanx in advance
If ProductName, ProductPrice and ProductSize are in the Detail table then you need to define some algorithm to "aggregate" them. If they are in the Order table you only need to "Group by" each of them as well:
Expand|Select|Wrap|Line Numbers
  1. SELECT o.OrderID, sum(d.Price*d.Quantity) as Total, o.ProductName, o.ProductPrice, o.ProductSize
  2. FROM Order o INNER JOIN OrderDetail d ON o.OrderId=d.OrderId
  3. GROUP BY o.OrderID, o.ProductName, o.ProductPrice, o.ProductSize
  4.  
Discount is similar to the original query if it is in the Detail table:
Expand|Select|Wrap|Line Numbers
  1. SELECT o.OrderID, sum(d.Price*d.Quantity-d.Discount)
  2. FROM Order o INNER JOIN OrderDetail d ON o.OrderId=d.OrderId
  3. GROUP BY o.OrderID
  4.  
Sep 25 '07 #6
Bangaru
16
Hi Jim,

I am not sure whether I should open a new thread for my problem...
since I found this post to be related with my doubts I reply here

In my case it is like I select a particular column with where conditions, it returns 8 rows like:

Result set

Defect
*******
Test1
Test2
Test3

I need the result in single row like as below

Defect
*******
Test1,Test2,Test3

where Defect is the particular column I am retrieving.
Can u please help me in this?
Sep 26 '07 #7
Dear Daniyal
To get OrderId wise sumup result from OrderDetail table, use this

Select OrderId,SUM(NVL(Price,0)*NVL(Quantity,0))
From OrderDetail
[Where <condition...date,code,range>]
Group by OrderId;

Regards
Javed Ameen
-------------------------------------------------------------------------------------
Hi,

I want to merge data from multi rows to one row.
For example,
I have two table one is 'ORDER' and one is 'ORDERDETAIL'.
What I want to do is when I want to get orders against 'user'...
I want to get 'ORDER' table INNER JOINED with 'ORDERDETAIL' but with TOTAL added and merged in a sigle row instead of multiple rows.
I want to generate this function in Stored Procedure in SQL 2005.
Can you help me about this?



1. ORDERDETAIL Table
OrderID......Price.....Quantity
A..................10............1
A..................14............2
B..................15............3
C..................20............4
C..................23............5
C..................24............6

2. RESULT TABLE (Output)
OrderID.........Total
A..................(10 * 1) + (14 * 2)
B..................(15 * 3)
C..................(20*4) + (23*5) + (24*6)
Sep 26 '07 #8

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

Similar topics

0
by: webtrack+googlegroups | last post by:
I'm trying automate a mail merge in VB.Net as a spike/proof-of-concept. The resulting mail merge document should look very roughly like this: <<Customer FullName>> <<Customer Address>> Dear...
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
2
by: Ruslan Shlain | last post by:
I have aproblem when i merge datasets. dsDS has 105 records dsMain has 147 record I am trying to to pour records in dsMain in to dsDS. Here is my code: DataSet dsDS =new DataSet(); ...
2
by: muntyanu | last post by:
Hi all, I have problem when merging existing DataTable into new dataset. DataSet ds = new DataSet(); while ( done ) { // fill myCustomDataSet.MyTable with data ds.Merge(...
11
by: UDBDBA | last post by:
Hi: This is a merge questions which has been posted and answered... in my case need more clairification when target table (tableB) matched multiple rows to be updated based on the ON condition...
5
by: Mark Chambers | last post by:
Hi there, Can anyone explain the following (very) simple scenario. 1) I make an exact copy of my "DataSet" and delete one record from a given table (in the copy) 2) I invoke...
7
by: Michel Esber | last post by:
Question About Merge DB2 V8 LUW FP 15. Hello, Consider table A (ID integer not null PK, Field2 varchar(50)). I have the existing following set of rows in the table:
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
24
by: Henry J. | last post by:
My app needs to insert thousand value rows into a mostly empty table (data are read from a file). I can either use inserts, or use merge. The advantage of using merge is that in the few cases...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.