Author |
Topic |
jbon
Starting Member
20 Posts |
Posted - 2012-10-25 : 08:15:15
|
Hi,lets say I have two tables INBOUND and OUTBOUND. They are related as 1 to many (one inbound can have several outbounds). INBOUND table have one column holding no of items delivered in. OUTBOUND table has one column holding no of items delivered out.Now in my query I want to keep track on the current saldo, how do I do this?Example:INBOUND tableItemID - NoOfItemsIn------------------------A - 100OUTBOUND tableItemID - NoOfItemsOut------------------------A - 20A - 40A - 40My query I want to result as belowQUERY RESULTItemID - NoOfItemsOut - SaldoInStock--------------------------------A - 20 - 80A - 40 - 40A - 40 - 0Note! SaldoInStock is not a column in table OUTBOUNDAll suggestions of possible solutions is highly appreciated. |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-10-25 : 08:50:48
|
with cte as (select *, seq = rownumber() over (order by NoOfItemsOut) from OUTBOUND)select i.ItemId, o.NoOfItemsOut, SaldoInStock = i.NoOfItemsIn - (select sum(NoOfItemsOut from cte o2 where o2.seq <= o.seq)from INBOUND ijoin cte oorder by o2.seq==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
jbon
Starting Member
20 Posts |
Posted - 2012-10-25 : 09:28:47
|
Thx, gets following error...'rownumber' is not a recognized built-in function name.any ideas?Is/meansSaldoInStock = i.NoOfItemsIn - (select sum(NoOfItemsOut from cte o2 where o2.seq <= o.seq)same asi.NoOfItemsIn - (select sum(NoOfItemsOut from cte o2 where o2.seq <= o.seq) as SaldoInStock? |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-10-25 : 09:32:58
|
change rownumber to row_number Too old to Rock'n'Roll too young to die. |
|
|
jbon
Starting Member
20 Posts |
Posted - 2012-10-26 : 07:53:34
|
[code]with cte as (select *, seq = row_number() over (order by NoOfItemsOut) from OUTBOUND)select i.ItemId, o.NoOfItemsOut, i.NoOfItemsIn - (select sum(NoOfItemsOut) from cte o2 where o2.seq <= o.seq)from INBOUND ijoin cte oorder by o2.seq[/code]Below gives me below error:Msg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword 'order'.Is it something wrong with/in the join statement? |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-10-26 : 08:58:38
|
You can copy / paste and run this:-- making testdata...declare @INBOUND table(ItemID varchar(10),NoOfItemsIn int)------------------------insert @INBOUNDselect 'A', 100declare @OUTBOUND table(ItemID varchar(10),NoOfItemsOut int)------------------------insert @OUTBOUNDselect 'A', 20 union allselect 'A', 40 union allselect 'A', 40----My query I want to result as below----QUERY RESULT----ItemID - NoOfItemsOut - SaldoInStock----------------------------------------A - 20 - 80----A - 40 - 40----A - 40 - 0-- nigel's solution...;with cte as (select *, seq = row_number() over (order by NoOfItemsOut) from @OUTBOUND)select i.ItemId, o.NoOfItemsOut, i.NoOfItemsIn - (select sum(NoOfItemsOut) from cte o2 where o2.seq <= o.seq)from @INBOUND ijoin cte o on i.ItemId = o.ItemIdorder by o.seq Too old to Rock'n'Roll too young to die. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-10-26 : 09:05:12
|
Test this please - I have modified the solution so it can handle more than one ItemId...-- making testdata...declare @INBOUND table(ItemID varchar(10),NoOfItemsIn int)------------------------insert @INBOUNDselect 'A', 100 union allselect 'B', 500declare @OUTBOUND table(ItemID varchar(10),NoOfItemsOut int)------------------------insert @OUTBOUNDselect 'A', 20 union allselect 'A', 40 union allselect 'A', 40 union allselect 'B', 450 union allselect 'B', 10----My query I want to result as below----QUERY RESULT----ItemID - NoOfItemsOut - SaldoInStock----------------------------------------A - 20 - 80----A - 40 - 40----A - 40 - 0-- nigel's solution...;with cte as (select *, seq = row_number() over (partition by ItemId order by NoOfItemsOut) from @OUTBOUND)select i.ItemId, o.NoOfItemsOut, i.NoOfItemsIn - (select sum(NoOfItemsOut) from cte o2 where o2.seq <= o.seq and o2.ItemId = o.ItemId)from @INBOUND ijoin cte o on i.ItemId = o.ItemIdorder by i.ItemId,o.seq Too old to Rock'n'Roll too young to die. |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-10-26 : 09:11:04
|
sorry I missed out the join criteriawith cte as (select *, seq = row_number() over (order by NoOfItemsOut) from OUTBOUND)select i.ItemId, o.NoOfItemsOut, i.NoOfItemsIn - (select sum(NoOfItemsOut) from cte o2 where o2.seq <= o.seq)from INBOUND ijoin cte oon i.ItemId = o.ItemIdorder by o2.seq==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-10-26 : 09:24:50
|
quote: Originally posted by nigelrivett sorry I missed out the join criteriawith cte as (select *, seq = row_number() over (order by NoOfItemsOut) from OUTBOUND)select i.ItemId, o.NoOfItemsOut, i.NoOfItemsIn - (select sum(NoOfItemsOut) from cte o2 where o2.seq <= o.seq)from INBOUND ijoin cte oon i.ItemId = o.ItemIdorder by o2.seq==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
The multi-part identifier "o2.seq" could not be bound.Did'nt you see my post(s)??? Too old to Rock'n'Roll too young to die. |
|
|
jbon
Starting Member
20 Posts |
Posted - 2012-10-26 : 09:36:01
|
Hi Webfred & nigelrivett,Thx for your help.Webfred - your code worked as i want, thx.Just curious... 'declare @INBOUND table(ItemID varchar(10),NoOfItemsIn int)' ... is this creating a ~"virtual" table? Really nice... |
|
|
|