Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sqlgurufrank
Starting Member
2 Posts |
Posted - 2006-02-04 : 05:45:06
|
| Have tried a lot, nothing worked! Need help desperately.I need the sql syntax for the following:Tables:CustomersCustomerID : CustomerName1 : Disney2 : ParamountProductsProductID : ProductName1 : sparkle2 : raindrop3 : MR16OrdersOrderID : OrderDate : CustomerID1 : 2005-10-12 : 22 : 2006-01-03 : 1OrderDetailsOrderDetailID : OrderID : ProductID : Quantity1 : 1 : 3 : 102 : 1 : 2 : 53 : 2 : 1 : 34 : 2 : 3 : 20DeliveryDetailsDeliveryDetailID : OrderDetailID : Quantity1 : 1 : 22 : 2 : 53 : 1 : 2Now I would like to see the backordersSomething like:OrderDetailID : CustomerName : ProductID : ProductName : QuantityOrdered: Quantity Delivered : BackOrder1 : Paramount : 3 : MR16 : 10 : 4 (group deliveries of orderdetail 1) : 6 (ordered-delivered)4 : Disney : MR16 : 20 : 0 : 20I hope somebody can help me with this one. |
|
|
Sprinjee
Starting Member
42 Posts |
Posted - 2006-02-04 : 06:47:57
|
| Something like this?:select od.OrderDetailID , cs.CustomerName, pr.ProductID , pr.ProductName, od.quantity, dl.quantity, (od.quantity - dl.quantity) as BackOrder from OrderDetails odinner join Orders Or on od.OrderID = Or.OrderIDinner join Customers Cs on Or.CustomerID = Cs.Customer.idinner join Products pr on pr.Productid = od.productidinner join DeliveryDetails dl on od.OrderDetailID = dl.OrderDetailID |
 |
|
|
sqlgurufrank
Starting Member
2 Posts |
Posted - 2006-02-04 : 07:47:31
|
Thanks for your help. We are closer to the solution now but still one challenge to go. Your solution does not calculate the backorder so I adapted to the following:select od.OrderDetailID , cs.CustomerName, pd.ProductID , pd.ProductName, od.quantity AS Ordered, SUM(dl.quantity) AS Delivered, (od.quantity - SUM(dl.quantity)) as BackOrder from odinner join ord on od.OrderID = ord.OrderIDinner join cs on ord.CustomerID = cs.CustomerIDinner join pd on pd.Productid = od.productidinner join dl on od.OrderDetailID = dl.OrderDetailIDGROUP BY dl.OrderDetailIDThose od.OrderDetailID backorders for which it can find deliveries. However I would also like to display in the same query those od.OrderDetailIDs for which it cannot find deliveries.Any Ideas?quote: Originally posted by Sprinjee Something like this?:select od.OrderDetailID , cs.CustomerName, pr.ProductID , pr.ProductName, od.quantity, dl.quantity, (od.quantity - dl.quantity) as BackOrder from OrderDetails odinner join Orders Or on od.OrderID = Or.OrderIDinner join Customers Cs on Or.CustomerID = Cs.Customer.idinner join Products pr on pr.Productid = od.productidinner join DeliveryDetails dl on od.OrderDetailID = dl.OrderDetailID
|
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-02-04 : 10:00:10
|
quote: Those od.OrderDetailID backorders for which it can find deliveries. However I would also like to display in the same query those od.OrderDetailIDs for which it cannot find deliveries.
Use LEFT JOINselect od.OrderDetailID, c.CustomerName, od.ProductID, p.ProductName, sum(od.Quantity) as QuantityOrdered, sum(isnull(dd.Quantity, 0)) as QuantityDelivered, sum(od.Quantity - isnull(dd.Quantity, 0)) as BackOrderfrom Orders o inner join OrderDetails od on o.OrderID = od.OrderID inner join Customers c on o.CustomerID = c.CustomerID inner join Products p on od.ProductID = p.ProductID left join DeliveryDetails dd on od.OrderDetailID = dd.OrderDetailIDwhere od.Quantity - isnull(dd.Quantity, 0) <> 0group by od.OrderDetailID, c.CustomerName, od.ProductID, p.ProductNameorder by od.OrderDetailID ----------------------------------'KH' |
 |
|
|
|
|
|
|
|