calculated field - difficult

C

clalc

I have to make a calculated field in the query where the value from one table
is deducted from the value in another table. Is that possible ? Here is the
example:
1st table (created from linking 2 tables):
Needs
ItemName quantityNeeded date
aaa 20 03-feb-10
aaa 5 10-feb-10
aaa 10 15-feb-10
ccc 3 5-feb-10
ccc 10 8-feb-10
ccc 7 12-feb-10

2nd table has data on hand:
OnHand
aaa 22
bbb 5
ccc 6

Now I have to do running total per date of items to be ordered. The result
would look something like this:
ItemName ToOrder Date
aaa 0 03-feb-10 (it's 22-20, left 2)
aaa 3 10-feb-10 (it's 5-2 )
aaa 10 15-feb-10
ccc 0 5-feb-10 (6-3, left 3)
ccc 7 8-feb-10 (10-3)
ccc 7 12-feb-10

Is that possible to do in a query ?
I'll appreciate any advise. Thank you.
 
K

KARL DEWEY

It will not work the way you have laid out the process. There isn't a rule
that will work with your numbers. You are operating with negative numbers.

Most businesses establish a stock level to maintain.

Your inventory would look like this FOR ITEM AAA --
1 FEB = 22
3 FEB = 2
10 FEB = -3
15 FEB = -13

When does the stock get replenished?

You have to show on-hand, order, issue, received.
 
C

clalc

I'm not sure if I explained properly but the whole idea is to track future
expenses for items to be ordered in the future. For this purpose I will not
have to manipulate table on hand. I just want to see how many items I need
to order and how much money I will need to have on each date when demand
arises. For not complicating the matter I omitted the money part, but this
is the idea behind the table Needs. Does it explain better the logic behind
it ?
Thank you for advise.
 
K

KARL DEWEY

This query will give you the results below --
SELECT Order_Fill.Item, Order_Fill.FillDate, (SELECT IIF((Stock.[On-Hand] -
Sum([XX].[QTY])) >0,0, (Stock.[On-Hand] - Sum([XX].[QTY]))*-1) FROM
Order_Fill AS [XX] WHERE [XX].FillDate <= Order_Fill.FillDate AND [XX].Item
= Order_Fill.Item) AS ToOrder
FROM Order_Fill LEFT JOIN Stock ON Order_Fill.Item = Stock.Item
ORDER BY Order_Fill.Item, Order_Fill.FillDate;

Item FillDate ToOrder
aaa 2/3/2010 0
aaa 2/10/2010 3
aaa 2/15/2010 13
ccc 2/5/2010 0
ccc 2/8/2010 7
ccc 2/12/2010 14

Can you show me matmatically how you would get the results you show?
 
C

clalc

Thank you very much for help. I'll let you know if it worked.

KARL DEWEY said:
This query will give you the results below --
SELECT Order_Fill.Item, Order_Fill.FillDate, (SELECT IIF((Stock.[On-Hand] -
Sum([XX].[QTY])) >0,0, (Stock.[On-Hand] - Sum([XX].[QTY]))*-1) FROM
Order_Fill AS [XX] WHERE [XX].FillDate <= Order_Fill.FillDate AND [XX].Item
= Order_Fill.Item) AS ToOrder
FROM Order_Fill LEFT JOIN Stock ON Order_Fill.Item = Stock.Item
ORDER BY Order_Fill.Item, Order_Fill.FillDate;

Item FillDate ToOrder
aaa 2/3/2010 0
aaa 2/10/2010 3
aaa 2/15/2010 13
ccc 2/5/2010 0
ccc 2/8/2010 7
ccc 2/12/2010 14

Can you show me matmatically how you would get the results you show?

--
Build a little, test a little.


clalc said:
I'm not sure if I explained properly but the whole idea is to track future
expenses for items to be ordered in the future. For this purpose I will not
have to manipulate table on hand. I just want to see how many items I need
to order and how much money I will need to have on each date when demand
arises. For not complicating the matter I omitted the money part, but this
is the idea behind the table Needs. Does it explain better the logic behind
it ?
Thank you for advise.
 
C

clalc

I have recreated the tables to suit query and tried to run query and I'm
asked for XX.QTY. Is that something not in order in the query ? When it
comes to numbers how I arrived at them, here it is.
ItemName ToOrder Date
aaa 0 03-feb-10 (it's 22-20, left 2)
aaa 3 10-feb-10 (it's 5-2j left 0 )
aaa 10 15-feb-10 (10-0 no more on hand)
ccc 0 5-feb-10 (6-3, left 3)
ccc 7 8-feb-10 (10-3, left 0)
ccc 7 12-feb-10 (7-0 no more on hand)

Is that helping ?





KARL DEWEY said:
This query will give you the results below --
SELECT Order_Fill.Item, Order_Fill.FillDate, (SELECT IIF((Stock.[On-Hand] -
Sum([XX].[QTY])) >0,0, (Stock.[On-Hand] - Sum([XX].[QTY]))*-1) FROM
Order_Fill AS [XX] WHERE [XX].FillDate <= Order_Fill.FillDate AND [XX].Item
= Order_Fill.Item) AS ToOrder
FROM Order_Fill LEFT JOIN Stock ON Order_Fill.Item = Stock.Item
ORDER BY Order_Fill.Item, Order_Fill.FillDate;

Item FillDate ToOrder
aaa 2/3/2010 0
aaa 2/10/2010 3
aaa 2/15/2010 13
ccc 2/5/2010 0
ccc 2/8/2010 7
ccc 2/12/2010 14

Can you show me matmatically how you would get the results you show?

--
Build a little, test a little.


clalc said:
I'm not sure if I explained properly but the whole idea is to track future
expenses for items to be ordered in the future. For this purpose I will not
have to manipulate table on hand. I just want to see how many items I need
to order and how much money I will need to have on each date when demand
arises. For not complicating the matter I omitted the money part, but this
is the idea behind the table Needs. Does it explain better the logic behind
it ?
Thank you for advise.
 
K

KARL DEWEY

I'm asked for XX.QTY.
Post the SQL you have in your query.

One way to do this is first to run a Rank in a Group query so each
transaction is numbered sequencial. Then in a subquery you could call out
the last trasaction results to subtract from running sum.

This is an example of Rank in a Group query --
SELECT Q.Group, Q.Item_no, Q.Points, (SELECT COUNT(*) FROM [Products] Q1
WHERE Q1.[Group] = Q.[Group]
AND Q1.[Points] < Q.[Points])+1 AS Rank
FROM Products AS Q
ORDER BY Q.Group, Q.Points;

--
Build a little, test a little.


clalc said:
I have recreated the tables to suit query and tried to run query and I'm
asked for XX.QTY. Is that something not in order in the query ? When it
comes to numbers how I arrived at them, here it is.
ItemName ToOrder Date
aaa 0 03-feb-10 (it's 22-20, left 2)
aaa 3 10-feb-10 (it's 5-2j left 0 )
aaa 10 15-feb-10 (10-0 no more on hand)
ccc 0 5-feb-10 (6-3, left 3)
ccc 7 8-feb-10 (10-3, left 0)
ccc 7 12-feb-10 (7-0 no more on hand)

Is that helping ?





KARL DEWEY said:
This query will give you the results below --
SELECT Order_Fill.Item, Order_Fill.FillDate, (SELECT IIF((Stock.[On-Hand] -
Sum([XX].[QTY])) >0,0, (Stock.[On-Hand] - Sum([XX].[QTY]))*-1) FROM
Order_Fill AS [XX] WHERE [XX].FillDate <= Order_Fill.FillDate AND [XX].Item
= Order_Fill.Item) AS ToOrder
FROM Order_Fill LEFT JOIN Stock ON Order_Fill.Item = Stock.Item
ORDER BY Order_Fill.Item, Order_Fill.FillDate;

Item FillDate ToOrder
aaa 2/3/2010 0
aaa 2/10/2010 3
aaa 2/15/2010 13
ccc 2/5/2010 0
ccc 2/8/2010 7
ccc 2/12/2010 14

Can you show me matmatically how you would get the results you show?

--
Build a little, test a little.


clalc said:
I'm not sure if I explained properly but the whole idea is to track future
expenses for items to be ordered in the future. For this purpose I will not
have to manipulate table on hand. I just want to see how many items I need
to order and how much money I will need to have on each date when demand
arises. For not complicating the matter I omitted the money part, but this
is the idea behind the table Needs. Does it explain better the logic behind
it ?
Thank you for advise.

:

It will not work the way you have laid out the process. There isn't a rule
that will work with your numbers. You are operating with negative numbers.

Most businesses establish a stock level to maintain.

Your inventory would look like this FOR ITEM AAA --
1 FEB = 22
3 FEB = 2
10 FEB = -3
15 FEB = -13

When does the stock get replenished?

You have to show on-hand, order, issue, received.

--
Build a little, test a little.


:

I have to make a calculated field in the query where the value from one table
is deducted from the value in another table. Is that possible ? Here is the
example:
1st table (created from linking 2 tables):
Needs
ItemName quantityNeeded date
aaa 20 03-feb-10
aaa 5 10-feb-10
aaa 10 15-feb-10
ccc 3 5-feb-10
ccc 10 8-feb-10
ccc 7 12-feb-10

2nd table has data on hand:
OnHand
aaa 22
bbb 5
ccc 6

Now I have to do running total per date of items to be ordered. The result
would look something like this:
ItemName ToOrder Date
aaa 0 03-feb-10 (it's 22-20, left 2)
aaa 3 10-feb-10 (it's 5-2 )
aaa 10 15-feb-10
ccc 0 5-feb-10 (6-3, left 3)
ccc 7 8-feb-10 (10-3)
ccc 7 12-feb-10

Is that possible to do in a query ?
I'll appreciate any advise. Thank you.
 
C

clalc

I just copied your query, but maybe I missed some part... Here it is:

SELECT Order_Fill.Item, Order_Fill.FillDate, (SELECT IIF((Stock.[On_Hand] -
Sum([XX].[QTY])) >0,0, (Stock.[On_Hand] - Sum([XX].[QTY]))*-1) FROM
Order_Fill AS [XX] WHERE [XX].FillDate <= Order_Fill.FillDate AND [XX].Item
= Order_Fill.Item) AS ToOrder
FROM Order_Fill LEFT JOIN Stock ON Order_Fill.Item = Stock.Item
ORDER BY Order_Fill.Item, Order_Fill.FillDate;


KARL DEWEY said:
Post the SQL you have in your query.

One way to do this is first to run a Rank in a Group query so each
transaction is numbered sequencial. Then in a subquery you could call out
the last trasaction results to subtract from running sum.

This is an example of Rank in a Group query --
SELECT Q.Group, Q.Item_no, Q.Points, (SELECT COUNT(*) FROM [Products] Q1
WHERE Q1.[Group] = Q.[Group]
AND Q1.[Points] < Q.[Points])+1 AS Rank
FROM Products AS Q
ORDER BY Q.Group, Q.Points;

--
Build a little, test a little.


clalc said:
I have recreated the tables to suit query and tried to run query and I'm
asked for XX.QTY. Is that something not in order in the query ? When it
comes to numbers how I arrived at them, here it is.
ItemName ToOrder Date
aaa 0 03-feb-10 (it's 22-20, left 2)
aaa 3 10-feb-10 (it's 5-2j left 0 )
aaa 10 15-feb-10 (10-0 no more on hand)
ccc 0 5-feb-10 (6-3, left 3)
ccc 7 8-feb-10 (10-3, left 0)
ccc 7 12-feb-10 (7-0 no more on hand)

Is that helping ?





KARL DEWEY said:
This query will give you the results below --
SELECT Order_Fill.Item, Order_Fill.FillDate, (SELECT IIF((Stock.[On-Hand] -
Sum([XX].[QTY])) >0,0, (Stock.[On-Hand] - Sum([XX].[QTY]))*-1) FROM
Order_Fill AS [XX] WHERE [XX].FillDate <= Order_Fill.FillDate AND [XX].Item
= Order_Fill.Item) AS ToOrder
FROM Order_Fill LEFT JOIN Stock ON Order_Fill.Item = Stock.Item
ORDER BY Order_Fill.Item, Order_Fill.FillDate;

Item FillDate ToOrder
aaa 2/3/2010 0
aaa 2/10/2010 3
aaa 2/15/2010 13
ccc 2/5/2010 0
ccc 2/8/2010 7
ccc 2/12/2010 14

Can you show me matmatically how you would get the results you show?

--
Build a little, test a little.


:

I'm not sure if I explained properly but the whole idea is to track future
expenses for items to be ordered in the future. For this purpose I will not
have to manipulate table on hand. I just want to see how many items I need
to order and how much money I will need to have on each date when demand
arises. For not complicating the matter I omitted the money part, but this
is the idea behind the table Needs. Does it explain better the logic behind
it ?
Thank you for advise.

:

It will not work the way you have laid out the process. There isn't a rule
that will work with your numbers. You are operating with negative numbers.

Most businesses establish a stock level to maintain.

Your inventory would look like this FOR ITEM AAA --
1 FEB = 22
3 FEB = 2
10 FEB = -3
15 FEB = -13

When does the stock get replenished?

You have to show on-hand, order, issue, received.

--
Build a little, test a little.


:

I have to make a calculated field in the query where the value from one table
is deducted from the value in another table. Is that possible ? Here is the
example:
1st table (created from linking 2 tables):
Needs
ItemName quantityNeeded date
aaa 20 03-feb-10
aaa 5 10-feb-10
aaa 10 15-feb-10
ccc 3 5-feb-10
ccc 10 8-feb-10
ccc 7 12-feb-10

2nd table has data on hand:
OnHand
aaa 22
bbb 5
ccc 6

Now I have to do running total per date of items to be ordered. The result
would look something like this:
ItemName ToOrder Date
aaa 0 03-feb-10 (it's 22-20, left 2)
aaa 3 10-feb-10 (it's 5-2 )
aaa 10 15-feb-10
ccc 0 5-feb-10 (6-3, left 3)
ccc 7 8-feb-10 (10-3)
ccc 7 12-feb-10

Is that possible to do in a query ?
I'll appreciate any advise. Thank you.
 
C

clalc

I have created ranking in my table with query :
SELECT Q.Item, Q.quantityNeeded, Q.FillDate, (SELECT COUNT(*) FROM
Order_Fill O_F WHERE O_F.Item = Q.Item AND O_F.FillDate < Q.FillDate)+1 AS
RANK
FROM Order_Fill AS Q
ORDER BY Q.Item, Q.FillDate;
and the result is:
Item quantityNeeded FillDate RANK
aaa 20 03/02/2010 1
aaa 5 10/02/2010 2
aaa 10 15/02/2010 3
ccc 3 05/02/2010 1
ccc 10 08/02/2010 2
ccc 7 12/02/2010 3

but I'm not sure how would I call last transaction ...


KARL DEWEY said:
Post the SQL you have in your query.

One way to do this is first to run a Rank in a Group query so each
transaction is numbered sequencial. Then in a subquery you could call out
the last trasaction results to subtract from running sum.

This is an example of Rank in a Group query --
SELECT Q.Group, Q.Item_no, Q.Points, (SELECT COUNT(*) FROM [Products] Q1
WHERE Q1.[Group] = Q.[Group]
AND Q1.[Points] < Q.[Points])+1 AS Rank
FROM Products AS Q
ORDER BY Q.Group, Q.Points;

--
Build a little, test a little.


clalc said:
I have recreated the tables to suit query and tried to run query and I'm
asked for XX.QTY. Is that something not in order in the query ? When it
comes to numbers how I arrived at them, here it is.
ItemName ToOrder Date
aaa 0 03-feb-10 (it's 22-20, left 2)
aaa 3 10-feb-10 (it's 5-2j left 0 )
aaa 10 15-feb-10 (10-0 no more on hand)
ccc 0 5-feb-10 (6-3, left 3)
ccc 7 8-feb-10 (10-3, left 0)
ccc 7 12-feb-10 (7-0 no more on hand)

Is that helping ?





KARL DEWEY said:
This query will give you the results below --
SELECT Order_Fill.Item, Order_Fill.FillDate, (SELECT IIF((Stock.[On-Hand] -
Sum([XX].[QTY])) >0,0, (Stock.[On-Hand] - Sum([XX].[QTY]))*-1) FROM
Order_Fill AS [XX] WHERE [XX].FillDate <= Order_Fill.FillDate AND [XX].Item
= Order_Fill.Item) AS ToOrder
FROM Order_Fill LEFT JOIN Stock ON Order_Fill.Item = Stock.Item
ORDER BY Order_Fill.Item, Order_Fill.FillDate;

Item FillDate ToOrder
aaa 2/3/2010 0
aaa 2/10/2010 3
aaa 2/15/2010 13
ccc 2/5/2010 0
ccc 2/8/2010 7
ccc 2/12/2010 14

Can you show me matmatically how you would get the results you show?

--
Build a little, test a little.


:

I'm not sure if I explained properly but the whole idea is to track future
expenses for items to be ordered in the future. For this purpose I will not
have to manipulate table on hand. I just want to see how many items I need
to order and how much money I will need to have on each date when demand
arises. For not complicating the matter I omitted the money part, but this
is the idea behind the table Needs. Does it explain better the logic behind
it ?
Thank you for advise.

:

It will not work the way you have laid out the process. There isn't a rule
that will work with your numbers. You are operating with negative numbers.

Most businesses establish a stock level to maintain.

Your inventory would look like this FOR ITEM AAA --
1 FEB = 22
3 FEB = 2
10 FEB = -3
15 FEB = -13

When does the stock get replenished?

You have to show on-hand, order, issue, received.

--
Build a little, test a little.


:

I have to make a calculated field in the query where the value from one table
is deducted from the value in another table. Is that possible ? Here is the
example:
1st table (created from linking 2 tables):
Needs
ItemName quantityNeeded date
aaa 20 03-feb-10
aaa 5 10-feb-10
aaa 10 15-feb-10
ccc 3 5-feb-10
ccc 10 8-feb-10
ccc 7 12-feb-10

2nd table has data on hand:
OnHand
aaa 22
bbb 5
ccc 6

Now I have to do running total per date of items to be ordered. The result
would look something like this:
ItemName ToOrder Date
aaa 0 03-feb-10 (it's 22-20, left 2)
aaa 3 10-feb-10 (it's 5-2 )
aaa 10 15-feb-10
ccc 0 5-feb-10 (6-3, left 3)
ccc 7 8-feb-10 (10-3)
ccc 7 12-feb-10

Is that possible to do in a query ?
I'll appreciate any advise. Thank you.
 
C

clalc

Sorry I used another name for QTY that's why I was getting this message. I
fixed it now, but I'm still not getting the right numbers.

clalc said:
I just copied your query, but maybe I missed some part... Here it is:

SELECT Order_Fill.Item, Order_Fill.FillDate, (SELECT IIF((Stock.[On_Hand] -
Sum([XX].[QTY])) >0,0, (Stock.[On_Hand] - Sum([XX].[QTY]))*-1) FROM
Order_Fill AS [XX] WHERE [XX].FillDate <= Order_Fill.FillDate AND [XX].Item
= Order_Fill.Item) AS ToOrder
FROM Order_Fill LEFT JOIN Stock ON Order_Fill.Item = Stock.Item
ORDER BY Order_Fill.Item, Order_Fill.FillDate;


KARL DEWEY said:
I'm asked for XX.QTY.
Post the SQL you have in your query.

One way to do this is first to run a Rank in a Group query so each
transaction is numbered sequencial. Then in a subquery you could call out
the last trasaction results to subtract from running sum.

This is an example of Rank in a Group query --
SELECT Q.Group, Q.Item_no, Q.Points, (SELECT COUNT(*) FROM [Products] Q1
WHERE Q1.[Group] = Q.[Group]
AND Q1.[Points] < Q.[Points])+1 AS Rank
FROM Products AS Q
ORDER BY Q.Group, Q.Points;

--
Build a little, test a little.


clalc said:
I have recreated the tables to suit query and tried to run query and I'm
asked for XX.QTY. Is that something not in order in the query ? When it
comes to numbers how I arrived at them, here it is.
ItemName ToOrder Date
aaa 0 03-feb-10 (it's 22-20, left 2)
aaa 3 10-feb-10 (it's 5-2j left 0 )
aaa 10 15-feb-10 (10-0 no more on hand)
ccc 0 5-feb-10 (6-3, left 3)
ccc 7 8-feb-10 (10-3, left 0)
ccc 7 12-feb-10 (7-0 no more on hand)

Is that helping ?





:

This query will give you the results below --
SELECT Order_Fill.Item, Order_Fill.FillDate, (SELECT IIF((Stock.[On-Hand] -
Sum([XX].[QTY])) >0,0, (Stock.[On-Hand] - Sum([XX].[QTY]))*-1) FROM
Order_Fill AS [XX] WHERE [XX].FillDate <= Order_Fill.FillDate AND [XX].Item
= Order_Fill.Item) AS ToOrder
FROM Order_Fill LEFT JOIN Stock ON Order_Fill.Item = Stock.Item
ORDER BY Order_Fill.Item, Order_Fill.FillDate;

Item FillDate ToOrder
aaa 2/3/2010 0
aaa 2/10/2010 3
aaa 2/15/2010 13
ccc 2/5/2010 0
ccc 2/8/2010 7
ccc 2/12/2010 14

Can you show me matmatically how you would get the results you show?

--
Build a little, test a little.


:

I'm not sure if I explained properly but the whole idea is to track future
expenses for items to be ordered in the future. For this purpose I will not
have to manipulate table on hand. I just want to see how many items I need
to order and how much money I will need to have on each date when demand
arises. For not complicating the matter I omitted the money part, but this
is the idea behind the table Needs. Does it explain better the logic behind
it ?
Thank you for advise.

:

It will not work the way you have laid out the process. There isn't a rule
that will work with your numbers. You are operating with negative numbers.

Most businesses establish a stock level to maintain.

Your inventory would look like this FOR ITEM AAA --
1 FEB = 22
3 FEB = 2
10 FEB = -3
15 FEB = -13

When does the stock get replenished?

You have to show on-hand, order, issue, received.

--
Build a little, test a little.


:

I have to make a calculated field in the query where the value from one table
is deducted from the value in another table. Is that possible ? Here is the
example:
1st table (created from linking 2 tables):
Needs
ItemName quantityNeeded date
aaa 20 03-feb-10
aaa 5 10-feb-10
aaa 10 15-feb-10
ccc 3 5-feb-10
ccc 10 8-feb-10
ccc 7 12-feb-10

2nd table has data on hand:
OnHand
aaa 22
bbb 5
ccc 6

Now I have to do running total per date of items to be ordered. The result
would look something like this:
ItemName ToOrder Date
aaa 0 03-feb-10 (it's 22-20, left 2)
aaa 3 10-feb-10 (it's 5-2 )
aaa 10 15-feb-10
ccc 0 5-feb-10 (6-3, left 3)
ccc 7 8-feb-10 (10-3)
ccc 7 12-feb-10

Is that possible to do in a query ?
I'll appreciate any advise. Thank you.
 
J

John W. Vinson

Sorry I used another name for QTY that's why I was getting this message. I
fixed it now, but I'm still not getting the right numbers.

If you would like help solving the problem, please post the current SQL of the
query, and an example of the data in your table, the wrong answer you're
getting, and the answer you desire to get.
 
C

clalc

Here are the tables:
Table Demand
Item Qty Date
aaa 10 03-mar-2010
aaa 7 05-mar-2010
aaa 5 12-mar-2010
ccc 8 06-mar-2010
ccc 12 09-mar-2010

Table Stock
Item Qty
aaa 12
bbb 9
ccc 6

the result should be table Net Demand
Item Qty Date (how to get column Qty)
aaa 0 03-mar-2010 (12-10, left 2)
aaa 5 05-mar-20 (7-2, left 0)
aaa 5 12-mar-2010 (5-0, left 0)
ccc 2 06-mar-2010 (8-6, left 0)
ccc 12 09-mar-2010 (12-0, left 0)

Here is SQL which does not give the results as above:
SELECT Demand.Item, Demand.Date, (SELECT IIF((Stock.[Qty] -
Sum([XX].[QTY])) >0,0, (Stock.[Qty] - Sum([XX].[QTY]))*-1) FROM
Demand AS [XX] WHERE [XX].Date <= Demand.Date AND [XX].Item
= Demand.Item) AS ToOrder
FROM Demand LEFT JOIN Stock ON Demand.Item = Stock.Item
ORDER BY Demand.Item, Demand.FillDate;
Would you have any suggestions ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Subquery with subraction - challenging 4
Data mining 1
Group By Month In A Query 4
calculating value in a query 1
"if... else" statement in MS Access 2
Query to Count If >= 1
DSUM 1
update query 5

Top