Results 1 to 3 of 3

Thread: Writing SQL [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Dreamland
    Posts
    216

    Resolved 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.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    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

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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.

    BOFH Now, BOFH Past, Information on duplicates

    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
  •  



Click Here to Expand Forum to Full Width