Results 1 to 5 of 5
  1. #1
    learning_graccess is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    40

    running or sub total using sql query in access


    I have a data similar to below
    date >> items >> qty
    23.05.2011 >> pen >> 3
    23.05.2011 >> pencil >> 10
    24.05.2011 >> paper >> 33
    25.05.2011 >> paper >> 23
    23.05.2011 >> paper >> 24

    Now I want total for 23.05.2011 and want output like below
    23.05.2011 >> pen >> 3
    23.05.2011 >> pencil >> 10
    23.05.2011 >> paper >> 24
    Total >> >> 37

    I heard that using report it's possible, in sql it's not possible in access. But i'm very new to access and I don't know except few sql query.
    And also after a long search, I found that using Union All command this is possible in sql too but I don't know how.

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Code:
    SELECT tblTest.SaleDate, tblTest.Item, Sum(tblTest.Qty) AS TotQty
    FROM tblTest
    GROUP BY tblTest.SaleDate, tblTest.Item
    HAVING ((SaleDate) = [Enter the Date])
    ORDER BY SaleDate, Item
    UNION ALL
    SELECT tblTest.SaleDate, 'Total' AS Item, Sum(tblTest.Qty) AS SumOfQty
    FROM tblTest
    GROUP BY tblTest.SaleDate, 'Total'
    HAVING ((SaleDate) = [Enter the Sale Date])
    ORDER BY SaleDate, Item
    This will give you what you want, I changed your 'date' field to 'saledate' because DATE is a reserved word in Access and will only cause you problems if you try to use it as a field name.

    Secondly this will prompt you for the [Enter the Sale Date] twice, because basically a union query is a series of queries whose results you are appending to a long list. To avoid the multiple prompts you can have your date input on a form and in the individual queries have a reference to that form/field like this:

    HAVING ((SaleDate) = [forms]![MyForm]![MyDate])

    Where myform and mydate would be the name of the form you have for the query setup and mydate would be the name of the text box that you're inputting your search date.

  3. #3
    learning_graccess is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    40
    thanks mate.
    I have a question. Suppose I want to view all dates and subtotal in each date and grand total at the end like below, is it possible in access.
    23.05.2011 >> pen >> 3
    23.05.2011 >> pencil >> 10
    23.05.2011 >> paper >> 24
    Total >> >> 37
    24.05.2011 >> paper >> 33
    Total >> >> 33
    25.05.2011 >> paper >> 23
    Total >> >> 23
    Grand Total >> 93

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    yes, it's the same methodology as the previous example just a little more complex you'd have three layers of to the union query instead of two.

    I'm not sure why you'd want to do this but it can be done. A far easier solution would be to build a with the subtotaling you want then export the report to an excel spreadsheet (which is what I suspect you're trying to do?)

  5. #5
    learning_graccess is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    40
    Quote Originally Posted by rpeare View Post
    yes, it's the same methodology as the previous example just a little more complex you'd have three layers of to the union query instead of two.

    I'm not sure why you'd want to do this but it can be done. A far easier solution would be to build a with the subtotaling you want then export the report to an excel spreadsheet (which is what I suspect you're trying to do?)
    Thank you again.

    The original data is more than 2,00,000 so it's not possible in excel. Access pivottable looks much easier but it takes ages to produce output maybe because of too much data. So the only way which possible right now is SQL query in Access which I can export as I want later in excel (only output). I don't know about any reports, forms, etc. in Access also.

    I would be happy if you give me code for the grand total

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Running total
    By lololthis in forum Queries
    Replies: 5
    Last Post: 06-21-2011, 04:14 PM
  2. Running Total in Subform
    By Scorpio11 in forum Forms
    Replies: 19
    Last Post: 07-03-2010, 05:44 PM
  3. Using running total in query to create graphs
    By maggioant in forum Queries
    Replies: 4
    Last Post: 10-02-2009, 05:58 AM
  4. Replies: 2
    Last Post: 09-30-2009, 09:40 AM
  5. Creating a Daily Running Total Query
    By seraph in forum Queries
    Replies: 0
    Last Post: 08-15-2009, 12:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums