| Author |
Topic  |
|
|
jbon
Starting Member
Sweden
18 Posts |
Posted - 10/25/2012 : 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 table ItemID - NoOfItemsIn ------------------------ A - 100
OUTBOUND table ItemID - NoOfItemsOut ------------------------ A - 20 A - 40 A - 40
My query I want to result as below QUERY RESULT ItemID - NoOfItemsOut - SaldoInStock -------------------------------- A - 20 - 80 A - 40 - 40 A - 40 - 0
Note! SaldoInStock is not a column in table OUTBOUND
All suggestions of possible solutions is highly appreciated. |
|
|
nigelrivett
Flowing Fount of Yak Knowledge
United Kingdom
3328 Posts |
Posted - 10/25/2012 : 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 i join cte o order 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
Sweden
18 Posts |
Posted - 10/25/2012 : 09:28:47
|
Thx, gets following error
...'rownumber' is not a recognized built-in function name.
any ideas?
Is/means
SaldoInStock = i.NoOfItemsIn - (select sum(NoOfItemsOut from cte o2 where o2.seq <= o.seq)
same as
i.NoOfItemsIn - (select sum(NoOfItemsOut from cte o2 where o2.seq <= o.seq) as SaldoInStock
?
|
 |
|
|
webfred
Flowing Fount of Yak Knowledge
Germany
8513 Posts |
Posted - 10/25/2012 : 09:32:58
|
change rownumber to row_number
Too old to Rock'n'Roll too young to die. |
 |
|
|
jbon
Starting Member
Sweden
18 Posts |
Posted - 10/26/2012 : 07:53:34
|
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 i
join cte o
order by o2.seq
Below gives me below error: Msg 156, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'order'.
Is it something wrong with/in the join statement? |
 |
|
|
webfred
Flowing Fount of Yak Knowledge
Germany
8513 Posts |
Posted - 10/26/2012 : 08:58:38
|
You can copy / paste and run this:
-- making testdata...
declare @INBOUND table(ItemID varchar(10),NoOfItemsIn int)
------------------------
insert @INBOUND
select 'A', 100
declare @OUTBOUND table(ItemID varchar(10),NoOfItemsOut int)
------------------------
insert @OUTBOUND
select 'A', 20 union all
select 'A', 40 union all
select '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 i
join cte o on i.ItemId = o.ItemId
order by o.seq
Too old to Rock'n'Roll too young to die. |
 |
|
|
webfred
Flowing Fount of Yak Knowledge
Germany
8513 Posts |
Posted - 10/26/2012 : 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 @INBOUND
select 'A', 100 union all
select 'B', 500
declare @OUTBOUND table(ItemID varchar(10),NoOfItemsOut int)
------------------------
insert @OUTBOUND
select 'A', 20 union all
select 'A', 40 union all
select 'A', 40 union all
select 'B', 450 union all
select '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 i
join cte o on i.ItemId = o.ItemId
order by i.ItemId,o.seq
Too old to Rock'n'Roll too young to die. |
 |
|
|
nigelrivett
Flowing Fount of Yak Knowledge
United Kingdom
3328 Posts |
Posted - 10/26/2012 : 09:11:04
|
sorry I missed out the join criteria
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 i join cte o on i.ItemId = o.ItemId order 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
Flowing Fount of Yak Knowledge
Germany
8513 Posts |
Posted - 10/26/2012 : 09:24:50
|
quote: Originally posted by nigelrivett
sorry I missed out the join criteria
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 i join cte o on i.ItemId = o.ItemId order 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
Sweden
18 Posts |
Posted - 10/26/2012 : 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...
|
 |
|
| |
Topic  |
|