Author |
Topic |
magmo
Aged Yak Warrior
558 Posts |
Posted - 2006-10-27 : 05:33:53
|
HiI have a problem in my query below. The query should retrieve all seperate articles and the total quantity ordered and the totalsum ordered. The problem is that there can be different price for the rows. Like this...NodeID Qty ProductPrice659 1 1,05659 2 2,05This is my query...<code>SELECT DISTINCT dbo.tbl_Order.NodeID, SUM(dbo.tbl_Order.Quantity) AS Qty, dbo.tbl_Products.Text, SUM(dbo.tbl_Order.Quantity) * MAX(dbo.tbl_Order.ProductPrice) AS TotalSumFROM dbo.tbl_Order INNER JOIN dbo.tbl_OrderInfo ON dbo.tbl_Order.CartID = dbo.tbl_OrderInfo.CartID INNER JOIN dbo.tbl_Login ON dbo.tbl_Order.UserUniqueID = dbo.tbl_Login.UID INNER JOIN dbo.tbl_Products ON dbo.tbl_Order.NodeID = dbo.tbl_Products.NodeIdWHERE (dbo.tbl_Order.DateOrdered BETWEEN CONVERT(DATETIME, '2006-10-01', 102) AND CONVERT(DATETIME, '2006-10-27', 102))GROUP BY dbo.tbl_Order.NodeID, dbo.tbl_Login.CustID, dbo.tbl_Products.TextHAVING (dbo.tbl_Login.CustID = 20)</code>When I run this query it looks fine for almost every row except one. NodeID 659 looks like this..NodeID Qty Text TotalSum659 2442 letter 1025640,00But if I run another query that looks like this..<code>SELECT DISTINCT dbo.tbl_Order.NodeID, SUM(dbo.tbl_Order.Quantity) AS Qty, SUM(dbo.tbl_Order.Quantity) * MAX(dbo.tbl_Order.ProductPrice) AS TotalSumFROM dbo.tbl_Order INNER JOIN dbo.tbl_Login ON dbo.tbl_Order.UserUniqueID = dbo.tbl_Login.UID INNER JOIN dbo.tbl_Products ON dbo.tbl_Order.NodeID = dbo.tbl_Products.NodeIdWHERE (dbo.tbl_Order.DateOrdered BETWEEN CONVERT(DATETIME, '2006-10-01', 102) AND CONVERT(DATETIME, '2006-10-27', 102))GROUP BY dbo.tbl_Order.ProductPrice, dbo.tbl_Order.NodeID, dbo.tbl_Login.CustID, dbo.tbl_Products.TextHAVING (dbo.tbl_Order.NodeID = 659)</code>I get this result..<code>NodeID Qty TotalSum659 1 420,00659 800 1000,00659 1641 1723,05</code>The Qty and Totalsum is here correct, but in my original question only the qty is correct.Could someone please check this and mayby see whats wrong here?RegardsM |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-27 : 05:37:01
|
You are using different GROUP BY's...Peter LarssonHelsingborg, Sweden |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-27 : 05:45:30
|
Try thisSELECT dbo.tbl_Order.NodeID, dbo.tbl_Order.UserUniqueID CustID, SUM(dbo.tbl_Order.Quantity) AS Qty, SUM(dbo.tbl_Order.Quantity * dbo.tbl_Order.ProductPrice) AS TotalSumFROM dbo.tbl_OrderINNER JOIN dbo.tbl_Products ON dbo.tbl_Order.NodeID = dbo.tbl_Products.NodeIdWHERE dbo.tbl_Order.DateOrdered BETWEEN '20061001' AND '20061027'-- AND dbo.tbl_Order.UserUniqueID = 20 AND dbo.tbl_Order.NodeID = 659GROUP BY dbo.tbl_Order.NodeID, dbo.tbl_Login.CustIDORDER BY dbo.tbl_Order.NodeID, dbo.tbl_Login.CustID Peter LarssonHelsingborg, Sweden |
|
|
magmo
Aged Yak Warrior
558 Posts |
Posted - 2006-10-27 : 07:13:44
|
Hi PeterThanks for your reply, one thing though. I need to modify the first query in my post that also use the tbl_OrderInfo and the tbl_Login table. Your suggestion missed those tables, maybe you changed the query based on the second query. Is it possible to change the first query so it give me the correct totalsum?Best regards |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-27 : 07:17:20
|
Why would you need to JOIN the tbl_OrderInfo? Nothing in the SELECT, GROUP BY or WHERE uses that table.Peter LarssonHelsingborg, Sweden |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-27 : 07:20:10
|
It depends on the data in the tables. For every record in a JOINed table that has multiple values, the JOIN returns too many records.Give us same sample data for each of the involved tables.Peter LarssonHelsingborg, Sweden |
|
|
magmo
Aged Yak Warrior
558 Posts |
Posted - 2006-10-27 : 07:41:48
|
Hi PeterYou are right I don't need the orderinfo table, it was there left there by mistake. I copied the query from another stored procedure where that table was neccesary to select based on information in that table.The tbl_Products contains information about the product, text and price for example, the tbl_Order also contains the price column but the difference between those two tables is that the tbl_products table contains the actual price of the product today. The tbl_Order contains the price of the product when it was ordered. So if a client ordered a product 2 months ago that product cost 20$ but 1 month later the product cost 25$. So my query should show a totalsum based on the price value that is in the tbl_Order table. I use this query to show statistic, therefore I need to show a totalsum of what the products cost when the order was made, not what the cost would be today based on what the product would cost today.Hope this all make sence. Do you need any more information, please let me know.Best regards |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-27 : 07:56:56
|
Give us same sample data for each of the involved tables.Peter LarssonHelsingborg, Sweden |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-27 : 08:18:55
|
Did you even try my last query?SELECT dbo.tbl_Order.NodeID, dbo.tbl_Products.Text, SUM(dbo.tbl_Order.Quantity) AS Qty, SUM(dbo.tbl_Order.Quantity * dbo.tbl_Order.ProductPrice) AS TotalSumFROM dbo.tbl_OrderINNER JOIN dbo.tbl_Login ON dbo.tbl_Order.UserUniqueID = dbo.tbl_Login.UIDINNER JOIN dbo.tbl_Products ON dbo.tbl_Order.NodeID = dbo.tbl_Products.NodeIdWHERE dbo.tbl_Order.DateOrdered BETWEEN '20061001' AND '20061027' AND dbo.tbl_Login.CustID = 20GROUP BY dbo.tbl_Order.NodeID, dbo.tbl_Products.Text Peter LarssonHelsingborg, Sweden |
|
|
magmo
Aged Yak Warrior
558 Posts |
Posted - 2006-10-27 : 08:22:38
|
Hi againHope this will do....tbl_OrderUserUniqueID NodeID Qty ProductPrice DateOrdred116 659 1 1.05 2006-10-20 00:00:00112 659 400 1.05 2006-10-12 00:00:00160 659 400 1.25 2006-08-21 00:00:0054 659 100 1.25 2006-06-09 00:00:0054 659 400 1.25 2006-06-13 00:00:00tbl_ProductsNodeID Text380 Book 1386 Book 2387 Book 3416 Book 4659 Book 5tbl_LoginUID CustID40 2041 2042 20116 2044 20Regards |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-27 : 08:41:25
|
[code]SELECT p.NodeID, p.[Text], SUM(o.Qty) Qty, SUM(o.Qty * o.ProductPrice) PriceFROM dbo.tbl_Order oINNER JOIN dbo.tbl_Products p ON p.NodeID = o.NodeIDINNER JOIN dbo.tbl_Login l ON l.UID = o.UserUniqueIDWHERE o.DateOrdered BETWEEN '20061001' AND '20061027' AND l.CustID = 20GROUP BY p.NodeID, p.[Text][/code]Peter LarssonHelsingborg, Sweden |
|
|
magmo
Aged Yak Warrior
558 Posts |
Posted - 2006-10-27 : 09:27:55
|
Hi PeterThat worked very well, Thank you very much! Really appreciate it.Best Regards |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-27 : 09:36:54
|
Good. Third time's a charm...Peter LarssonHelsingborg, Sweden |
|
|
|