|
-
Jul 31st, 2007, 10:41 PM
#1
Thread Starter
Addicted Member
Writing SQL [RESOLVED]
I'm using MS Access 2002
I have a table called Table 1
In table1 there are colums called "days", "month", & "spent"
How do i write an sql to view the total sum spent for each month. (the "spent" in my database is filled for each day - so 30days = 30spents * 12 months)
The output should be just two columns - month & spent. And it should have only 12 rows - January to december.
I'm using this for now
Code:
SELECT Sum(spent) AS January
FROM table1
WHERE (((table1.month)="January"));
But i want all the months in one query.
Is there a way to get what i want, or is making 12 queries then combining them the only way?
Last edited by freeze_sr; Aug 1st, 2007 at 08:43 PM.
-
Aug 1st, 2007, 05:09 AM
#2
Frenzied Member
Re: Writing SQL
I wouldn't use month as a field name since it's also the name of a function in VB.
Anyway, instead of your WHERE clause, try a GROUP BY month.
Tengo mas preguntas que contestas
-
Aug 1st, 2007, 05:26 AM
#3
Re: Writing SQL
To expand on salvelinus's post...
You want the montha nd a (sum) total of spend, yet in your Sql you only bring back one sum field and filter on a where.
Tru this works, but you'd need sub queries for each month.
As salvelinus posted, use a group by.
Code:
SELECT
[table1].[month]
,Sum(spent) AS Total
FROM
table1
GROUP BY
[table1].[month]
When you use group by you must list all fields that are not aggregate functions (sum, max, min etc).
Try the above then mess around with the sql, see what happens.
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|