PDA

View Full Version : Solved: SQL QUERY HELP!



faye
02-28-2006, 07:46 PM
Hi all, i am having a big headache figuring out this problem.
I have a table like this:

Table Name:Built

Part No.........................Date...............................Sum Of Built
ver10001....................2/1/2006...............................10000
ver10001....................3/1/2006...............................15000
ver10001....................4/1/2006...............................20000
ver10001....................5/1/2006...............................25000
ver10001....................6/1/2006...............................30000

And another table like this:

Table Name: Demand

Part No..........................Date................................Sum of Demand
ver10001.......................1/1/2006.................................2000
ver10001.......................2/1/2006.................................4000
ver10001.......................3/1/2006.................................6000
ver10001.......................4/1/2006.................................8000
ver10001.......................5/1/2006................................10000
ver10001.......................6/1/2006................................12000

Here's the question:

How can i use a sql query to subtract the [sum of Demand] from the [Sum of Built] Where the Date for the Table [Demand] is one month earlier than the Date for the Table [Built].

The resulted output should look like this:

Part No......................Date...........................Calculated Value
ver10001.................2/1/2006...............................8000
ver10001.................3/1/2006..............................11000
ver10001.................4/1/2006..............................14000
ver10001.................5/1/2006..............................17000
ver10001.................6/1/2006..............................20000


Please help!

Thank you

geekgirlau
02-28-2006, 08:18 PM
Start with a sub query:

SELECT [Built].[Part No], DateAdd("m",-1,[Date]) AS DemandDate, [Built].[Sum Of Built]
FROM Built;

Then a main query:

SELECT Demand.[Part No], Demand.Date, [Sum Of Built]-[Sum of Demand] AS Calc
FROM Demand INNER JOIN sqry_Built ON (Demand.Date = sqry_Built.DemandDate) AND (Demand.[Part No] = sqry_Built.[Part No]);

faye
02-28-2006, 08:19 PM
THANKS! It Worked!

geekgirlau
02-28-2006, 11:15 PM
My pleasure - make sure you mark the thread as "Solved" (look under Thread Tools at the top of the page)

XLGibbs
03-01-2006, 03:29 PM
Seems a lot like the example I provided in Access when the same exact question was posed a few days ago. Sorry i didn't get back to you in time to clarify the process Faye.

Glad you got it worked out..!