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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SQL query help

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 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
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 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.
Go to Top of Page

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/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

?
Go to Top of Page

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.
Go to Top of Page

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 i
join cte o
order by o2.seq[/code]

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?
Go to Top of Page

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 @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.
Go to Top of Page

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 @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.
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-10-26 : 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.
Go to Top of Page

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 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.
Go to Top of Page

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...
Go to Top of Page
   

- Advertisement -