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 |
|
GunYan
Starting Member
3 Posts |
Posted - 2004-07-27 : 05:52:50
|
| Hi all!I have a Warehouse table and a table showing stockinformation for each warehouse and each product. Sometimes one warehouse don't have a certain article in the stocktable and tha't when i get my problem.Everything works fine when i only have one product in the Stock table but when i populate my stocktable with other products the rows with Null values on the right disappears. Do anyone have a solution for this. I've been trying for hours to find one... :-( I need all the warehouses on the left even if there are no matching rows on the right.Thanx in advance!SELECT *FROM WarehouseLEFT OUTER JOIN StockSum ON StockSum.WarehouseNr = Warehouse.WarehouseNumber AND StockSum.Site = Warehouse.SiteWHERE (Warehouse.Site = 'siba.se') AND (StockSum.ProductNr = 'SEPC281' OR internetuser.Lagersaldo.ProductNr IS NULL) |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2004-07-27 : 06:04:16
|
| (Warehouse.Site = 'siba.se') -- This will limit the result to just one warehouse regardlessAND(StockSum.ProductNr = 'SEPC281' ORinternetuser.Lagersaldo.ProductNr IS NULL) -- Not sure why you are including a totally different table at this point-- The last bit should probably be (StockSum.ProductNr = 'SEPC281' OR StockSum.ProductNr IS NULL)-------Moo. :) |
 |
|
|
GunYan
Starting Member
3 Posts |
Posted - 2004-07-27 : 06:48:52
|
| Sorry for not being clear enough. I translated the column and table names from swedish but apparently i forgot some.Site can have five different values but in this case it's 'siba.se'. Around 50 warehouses share the same sitename. In this question i only want warehouses that have the site 'siba.se'. Here it's the right translation. My problem is still unsolved. Thanx anyway!SELECT *FROMWarehouseLEFT OUTER JOINStockSum ON StockSum.WarehouseNr = Warehouse.WarehouseNumber AND StockSum.Site = Warehouse.SiteWHERE(Warehouse.Site = 'siba.se') AND (StockSum.ProductNr = 'SEPC281' ORStocksum.ProductNr IS NULL) |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2004-07-27 : 07:16:37
|
The problem here is that when you add a WHERE condition on the insignificant table in a left join (i.e. the right table), SQL Server treats this as an INNER JOIN rather than a LEFT JOIN. The solution is simply to move any WHERE conditions on the StockSum table to the JOIN predicate. So your statement would be:SELECT *FROM WarehouseLEFT OUTER JOIN StockSum ON StockSum.WarehouseNr = Warehouse.WarehouseNumber AND StockSum.Site = Warehouse.SiteAND (StockSum.ProductNr = 'SEPC281' OR Stocksum.ProductNr IS NULL)WHERE (Warehouse.Site = 'siba.se') OS |
 |
|
|
GunYan
Starting Member
3 Posts |
Posted - 2004-07-27 : 07:54:23
|
| Thanx a lot! That solved it! |
 |
|
|
|
|
|