Author |
Topic |
Daniel.V
Starting Member
2 Posts |
Posted - 2012-11-29 : 17:09:52
|
Hello,I have a special inner join question:I have two Tabels:WarehouseWARHOUSE|LOCATION|ITEM|QUANTITY000 |1 |XY |2000 |1 |XY |1000 |2 |XY |1010 | |XX |2Warhouse ConfigWARHOUSE|LOCATION|KIND000 |1 |BULK000 |2 |RECEIVEI need a Statement to get a Sum of all Items of the Warehouse (group by Items). But only with Items of the BULK location and the Items without locations.The result has to be:ITEM|SUM(QUANTITY)XY |3XX |2Can I create for this one Statement?Thanks,Daniel |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2012-11-29 : 17:35:07
|
this seems to work:declare @Warehouse table(WARHOUSE char(3), LOCATION int, ITEM char(2),QUANTITY int)insert @warehouseselect '000' ,1 ,'XY' ,2 union allselect '000' ,1 ,'XY' ,1 union allselect '000' ,2 ,'XY' ,1 union allselect '010' ,null ,'XX' ,2declare @WarehouseConfig table(WARHOUSE char(3), LOCATION int, KIND varchar(12))insert @WarehouseConfigselect '000' ,1 ,'BULK' union allselect '000' ,2 ,'RECEIVE'select w.item ,sum(quantity) [sum(Quantity)]from @warehouse wleft outer join @warehouseConfig c on c.warhouse = w.warhouse and c.location = w.locationwhere (c.kind = 'bulk' or c.warhouse is null)group by w.itemorder by 2 descOUTPUT:item sum(Quantity)---- -------------XY 3XX 2 Be One with the OptimizerTG |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-11-29 : 19:42:09
|
quote: Originally posted by TG this seems to work:declare @Warehouse table(WARHOUSE char(3), LOCATION int, ITEM char(2),QUANTITY int)insert @warehouseselect '000' ,1 ,'XY' ,2 union allselect '000' ,1 ,'XY' ,1 union allselect '000' ,2 ,'XY' ,1 union allselect '010' ,null ,'XX' ,2declare @WarehouseConfig table(WARHOUSE char(3), LOCATION int, KIND varchar(12))insert @WarehouseConfigselect '000' ,1 ,'BULK' union allselect '000' ,2 ,'RECEIVE'select w.item ,sum(quantity) [sum(Quantity)]from @warehouse wleft outer join @warehouseConfig c on c.warhouse = w.warhouse and c.location = w.locationwhere (c.kind = 'bulk' or c.warhouse is null)group by w.itemorder by 2 descOUTPUT:item sum(Quantity)---- -------------XY 3XX 2 Be One with the OptimizerTG
Shouldn't it be c.Location is null |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-11-30 : 00:49:33
|
[code]declare @Warehouse table(WARHOUSE char(3), LOCATION int, ITEM char(2),QUANTITY int)insert @warehouseselect '000' ,1 ,'XY' ,2 union allselect '000' ,1 ,'XY' ,1 union allselect '000' ,2 ,'XY' ,1 union allselect '010' ,null ,'XX' ,2declare @WarehouseConfig table(WARHOUSE char(3), LOCATION int, KIND varchar(12))insert @WarehouseConfigselect '000' ,1 ,'BULK' union allselect '000' ,2 ,'RECEIVE'SELECT Item, SUM(quantity) total FROM @WarehouseWHERE LOCATION = (SELECT LOCATION FROM @WarehouseConfig WHERE KIND = 'BULK') OR LOCATION IS NULLGROUP BY ITEM[/code]--Chandu |
|
|
Daniel.V
Starting Member
2 Posts |
Posted - 2012-11-30 : 08:25:04
|
Thanks a lot. It works great!!!Daniel |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2012-11-30 : 11:25:39
|
quote: Originally posted by sodeepShouldn't it be c.Location is null
Shouldn't matter. Because left outer join on both columns if location is null in the source table then the row will be excluded meaning warhouse will also be null for the join result.Be One with the OptimizerTG |
|
|
|
|
|