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)
7 23822
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)
-
SELECT o.OrderID, sum(d.Price*d.Quantity)
-
FROM Order o INNER JOIN OrderDetail d ON o.OrderId=d.OrderId
-
GROUP BY o.OrderID
-
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 -
-
CREATE FUNCTION dbo.UDF_OrderDetail
-
( @OrderID char(1) )
-
RETURNS varchar(1000)
-
AS
-
BEGIN
-
DECLARE @OrderDetail varchar(1000), @Delimiter char
-
SET @Delimiter = '+'
-
SELECT @OrderDetail = COALESCE(@OrderDetail + @Delimiter, '') + strPQ FROM
-
(SELECT OrderID, Price, Quantity, '(' + LTRIM(STR(Price)) + ' * ' + LTRIM(STR(Quantity)) + ')' AS strPQ
-
FROM dbo.OrderDetail where OrderID=@OrderID) derived
-
WHERE OrderID=@OrderID
-
RETURN ( SELECT REPLACE(@OrderDetail,')+(',') + (') AS [Order_Detail])
-
END
-
Then to use it in a query or view call it like this -
-
SELECT DISTINCT OrderID, dbo.UDF_OrderDetail(OrderID) AS Total
-
FROM dbo.OrderDetail
-
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
-
SELECT o.OrderID, sum(d.Price*d.Quantity)
-
FROM Order o INNER JOIN OrderDetail d ON o.OrderId=d.OrderId
-
GROUP BY o.OrderID
-
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
-
SELECT o.OrderID, sum(d.Price*d.Quantity)
-
FROM Order o INNER JOIN OrderDetail d ON o.OrderId=d.OrderId
-
GROUP BY o.OrderID
-
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
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: -
SELECT o.OrderID, sum(d.Price*d.Quantity) as Total, o.ProductName, o.ProductPrice, o.ProductSize
-
FROM Order o INNER JOIN OrderDetail d ON o.OrderId=d.OrderId
-
GROUP BY o.OrderID, o.ProductName, o.ProductPrice, o.ProductSize
-
Discount is similar to the original query if it is in the Detail table: -
SELECT o.OrderID, sum(d.Price*d.Quantity-d.Discount)
-
FROM Order o INNER JOIN OrderDetail d ON o.OrderId=d.OrderId
-
GROUP BY o.OrderID
-
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?
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)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
=...
|
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();
...
|
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(...
|
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...
|
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...
|
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:
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |