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 |
|
mapidea
Posting Yak Master
124 Posts |
Posted - 2010-01-27 : 14:42:53
|
| I have a query. It has to return All the warehouse name from table [Warehouses].It returns the on_hand value from the Product_Warehouse table.Then there is table Order_Items_Shipping_Comments which maintains the allocations. It is not returning all the warehouse name.SELECT w.[warehouse_type_id], w.[name], CONVERT(DECIMAL(10,0), ISNULL(pw.on_hand,0)) AS on_hand, CONVERT(DECIMAL(10,0), (ISNULL(SUM(pw.on_hand),0) - ISNULL(SUM(ioisc.allocated),0))) AS availableTobeAllocated FROM [Warehouses] AS w LEFT JOIN Product_Warehouse AS pw ON pw.warehouse_type_id = w.warehouse_type_idLEFT JOIN (SELECT warehouse_id, ISNULL(SUM(quantity),0) as allocated FROM Order_Items_Shipping_Comments scWHERE sc.product_id=@product_idGroup By warehouse_id) ioisc ON ioisc.warehouse_id = pw.warehouse_type_idWHERE pw.product_id = @product_idGROUP BY w.[warehouse_type_id], w.[name], pw.on_handORDER BY on_hand DESC |
|
|
DP978
Constraint Violating Yak Guru
269 Posts |
Posted - 2010-01-27 : 14:51:05
|
| SELECT w.[warehouse_type_id],wn.[name],CONVERT(DECIMAL(10,0), ISNULL(pw.on_hand,0)) AS on_hand,CONVERT(DECIMAL(10,0), (ISNULL(SUM(pw.on_hand),0) - ISNULL(SUM(ioisc.allocated),0))) AS availableTobeAllocated FROM ( Select Disinct name from [Warehouses]) wnLeft join [Warehouses] w on wn.Name = w.NameLEFT JOIN Product_Warehouse AS pw ON pw.warehouse_type_id = w.warehouse_type_idLEFT JOIN (SELECT warehouse_id, ISNULL(SUM(quantity),0) as allocated FROM Order_Items_Shipping_Comments scWHERE sc.product_id=@product_idGroup By warehouse_id) ioisc ON ioisc.warehouse_id = pw.warehouse_type_idWHERE pw.product_id = @product_idGROUP BYw.[warehouse_type_id],w.[name],pw.on_handORDER BY on_hand DESCShould be ok. |
 |
|
|
mapidea
Posting Yak Master
124 Posts |
Posted - 2010-01-27 : 15:05:14
|
| Thanks for your reply.That didn't quite workif I comment the WHERE pw.product_id = @product_id it returns all the records.SELECT w.[warehouse_type_id], w.[name], CONVERT(DECIMAL(10,0), ISNULL(pw.on_hand,0)) AS on_hand, CONVERT(DECIMAL(10,0), (ISNULL(pw.on_hand,0) - ISNULL(ioisc.allocated,0))) AS availableTobeAllocated FROM [Warehouses] AS w LEFT JOIN Product_Warehouse AS pw ON pw.warehouse_type_id = w.warehouse_type_idLEFT JOIN (SELECT warehouse_id, ISNULL(SUM(quantity),0) as allocated FROM Order_Items_Shipping_Comments scWHERE sc.product_id=@product_idGroup By warehouse_id) ioisc ON ioisc.warehouse_id = pw.warehouse_type_id--WHERE pw.product_id = @product_idGROUP BY w.[warehouse_type_id], w.[name], pw.on_hand, ioisc.allocatedORDER BY on_hand DESCReturns-2 Missing Inventory 16 16-1 Default 16 16213 Pending Returns 16 16218 DENVER 16 16222 MANKATO 16 16220 KANSAS CITY 16 16221 KINGMAN 16 16214 INDURFA 16 16216 CLEVELAND-WESTLAKE 16 16-1 Default 15 15217 CORSICANA 15 15-1 Default 9 9But if I uncomment it returnswarehouse_type_id name on_hand availableTobeAllocated----------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------- ----------------------------------------1 Default 16 16 |
 |
|
|
DP978
Constraint Violating Yak Guru
269 Posts |
Posted - 2010-01-27 : 15:08:28
|
| Right, because your first table is the one you are getting the name from, when you put the where clause it limits the roms in that initial table.Let me look at my code and see if I can adjust |
 |
|
|
mapidea
Posting Yak Master
124 Posts |
Posted - 2010-01-27 : 15:15:17
|
| This seems to work........ ThanksSELECT DISTINCT w.[warehouse_type_id], w.[name], CONVERT(DECIMAL(10,0), ISNULL(pw.on_hand,0)) AS on_hand, CONVERT(DECIMAL(10,0), (ISNULL(pw.on_hand,0) - ISNULL(ioisc.allocated,0))) AS availableTobeAllocated FROM [Warehouses] AS w LEFT JOIN Product_Warehouse AS pw ON pw.warehouse_type_id = w.warehouse_type_id AND pw.product_id = @product_idLEFT JOIN (SELECT warehouse_id, ISNULL(SUM(quantity),0) as allocated FROM Order_Items_Shipping_Comments scWHERE sc.product_id=@product_idGroup By warehouse_id) ioisc ON ioisc.warehouse_id = pw.warehouse_type_idORDER BY on_hand DESC |
 |
|
|
DP978
Constraint Violating Yak Guru
269 Posts |
Posted - 2010-01-27 : 15:24:05
|
| Hmm try and implement something like this, someone else may have some more hints since I dont want you to get back wrong resultsDeclare @t1 table ( id int , name varchar(20))Insert @t1Select 1, 'A' UNION ALLSelect 2, 'B' UNION ALLSelect 3, 'C' Declare @t2 table ( id int , numvalue int )Insert @t2Select 1, 60 UNION ALLSelect 1, 5 UNION ALLSelect 3, 40 Select a.name , sum(b.numvalue) as mySum from @t1 a FULL OUTER join @t2 b on a.id = b.id group by a.name |
 |
|
|
|
|
|
|
|