Unable to make GroupBy work as intended

Please support our MS Access and FileMaker Pro advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Feb 2006
Posts: 50
Reputation: ultra vires is an unknown quantity at this point 
Solved Threads: 5
ultra vires ultra vires is offline Offline
Junior Poster in Training

Unable to make GroupBy work as intended

 
0
  #1
Aug 18th, 2009
Hey guys need your help here. I will go ahead and describe my problem and what i intend and what i am getting.

Platform for SQL Queries : MS Access

There are 2 identical table in terms of table structure, they contain information ( RAW Data) about month, money i spent in that month and for what thing i spent my money on.

Now what i want to do is compare these two tables and subtract total value spent.

1st table has data from year 2005
2nd table has data from year 2006

The response i am getting is actually adding "Total Value" multiple times. How do i go about this ?

SELECT s2.[Month],SUM(s2.[Total Value]), SUM(d.[Total Value]), s2.[Description_det] 
FROM  Sheet2 AS s2
INNER JOIN 
(
SELECT s1.[Month],SUM(s1.[Total Value]), s1.[Description_det] FROM  Sheet1 AS s1
GROUP BY s1.[Month],s1.[Description_det]
)d ON  s2.[Description_det]=d.[Description_det] AND s2.[Month] = d.[Month]
GROUP BY s2.[Month],s2.[Description_det]
;
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 306
Reputation: timothybard is an unknown quantity at this point 
Solved Threads: 26
timothybard's Avatar
timothybard timothybard is offline Offline
Posting Whiz

Re: Unable to make GroupBy work as intended

 
0
  #2
Aug 18th, 2009
Can you tell me what the fields in each of the tables are and what the table d contains?

It seems like the issue you are having is that is that your join is creating an instance where each record for a particular month in your first table is being joined to each record for the same month in the second table. The easiest solution is to create three separate queries: the first will take the data in your first table, group by month and sum by total value; the second will do the same except for the second table; the third query will take both of those queries, join on month and then display the total value from each query. You can also perform the subtraction in the third query.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 50
Reputation: ultra vires is an unknown quantity at this point 
Solved Threads: 5
ultra vires ultra vires is offline Offline
Junior Poster in Training

Re: Unable to make GroupBy work as intended

 
0
  #3
Aug 18th, 2009
Yes sir that's exactly what i want to do.
Table Structure:
{
Id (number)
Month ( Text)
Total Value(Number)
Description_det(Tex)
}

Both the tables have this same structure
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 306
Reputation: timothybard is an unknown quantity at this point 
Solved Threads: 26
timothybard's Avatar
timothybard timothybard is offline Offline
Posting Whiz

Re: Unable to make GroupBy work as intended

 
0
  #4
Aug 18th, 2009
Try creating the following three queries. If you use a name other than the ones I provide, then you will need to update the last query to refer to the names you used for the first two.

Query1:
  1. SELECT Sheet1.MONTH, Sum(Sheet1.[Total Value]) AS [SumOfTotal Value]
  2. FROM Sheet1
  3. GROUP BY Sheet1.MONTH;

Query2:
  1. SELECT Sheet2.MONTH, Sum(Sheet2.[Total Value]) AS [SumOfTotal Value]
  2. FROM Sheet2
  3. GROUP BY Sheet2.MONTH;

Query3:
  1. SELECT Query1.MONTH, Query1.[SumOfTotal Value], Query2.[SumOfTotal Value], [Query1].[SumOfTotal Value]-[Query2].[SumOfTotal Value] AS Diff
  2. FROM Query1 LEFT JOIN Query2 ON Query1.MONTH = Query2.MONTH;
Last edited by peter_budo; Aug 18th, 2009 at 4:48 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 50
Reputation: ultra vires is an unknown quantity at this point 
Solved Threads: 5
ultra vires ultra vires is offline Offline
Junior Poster in Training

Re: Unable to make GroupBy work as intended

 
0
  #5
Aug 21st, 2009
Ten minutes after reading your 1st post i realized how simple it was and how stupid i was

Thanx for that suggestion and your 2nd post
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC