Month Over Month Differences

dsellers1

Registered User.
Local time
Today, 14:13
Joined
May 19, 2009
Messages
39
So I have a very simply table: Column1 is 'Name", Column2 is "Sales", Column3 is "Month"

I am needing to run a query that will only show the differences month over month for each of the names. As an example, the table shows:

David 5 January
David 6 February
David 7 March

When running a query for March, I would want it to show the name, David, and the difference between February and March, +1. I would think there would be a formula needed for the query criteria but I'm drawing a blank.

If it is not easily explained, let me know and I will throw a quick database together so you can review it (my currentdatabase is a bit more complex and not needed for this issue)
 
It might work to use a subquery here, where essentially you write a second SELECT query to provide a single value to the parent query ...
Code:
SELECT Sales, Sales - ( 
    SELECT Sales 
    FROM tSales 
    WHERE Name = [prmName] AND Month = [prmMonth] - 1 ) As DeltaSales
FROM tSales
WHERE Name = [prmName] AND Month = [prmMonth]
This is the logic anyway. But you have some challenges if you're actually storing the month as a string. Also, one month less than January is December, so the math is 1 - 1 = 12. I'd look into the DateAdd() function and the MonthName() function.
HTH
 
Ah, you may be right on the subquery. Going to play around with this idea for a while and see what I come up with. Thanks for the info!
 

Users who are viewing this thread

Back
Top Bottom