Results 1 to 8 of 8
  1. #1
    theghost is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jan 2018
    Posts
    4

    How to select all from a table and also sum a column grouped by another column in same table

    I have a table with the following fields.(Ticketnum,Date,Time,Day,Weight,Lot,Product s,Description,Rail,Operator). I am trying to create a query in Access 2016 to pass to a recordset in a vb6 application. I will be using the data in a report. I need everything out of the table (SELECT *) and also the sum of the weight column grouped by the rail column where the date field = a date that will be passed to the query from the vb6 app. I have created two separate queries at the moment. Each one will run independently from each other. One Selects * where date = the date that is passed. I get good data.
    SELECT ScaleData.*
    FROM ScaleData
    WHERE (((ScaleData.Date)=[prmDate]));


    The second will give me the total weights for the weight field grouped by the rail field.
    SELECT Sum(ScaleData.Weight) AS ['Total'], ScaleData.[Rail]FROM ScaleData
    WHERE (((ScaleData.[Date])=[prmDate]))
    GROUP BY ScaleData.[Rail];

    When I create a query with both of the above queries and run it I get duplicate rows. I do not get duplicate rows when I run them independent of each other.


    SELECT DISTINCTROW Select_Data.*, Select_Rail_Totals.*
    FROM Select_Data, Select_Rail_Totals;
    Click image for larger version. 

Name:	Output.JPG 
Views:	14 
Size:	118.6 KB 
ID:	31999

    Can anyone help me with getting the data I need without duplicate rows? Any help would be appreciated.

    Thanks,

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    You need a join between the tables, presumably on the Rail field.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    theghost is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jan 2018
    Posts
    4
    Quote Originally Posted by pbaldy View Post
    You need a join between the tables, presumably on the Rail field.
    Hello pbaldy,

    Thank you for your response. Could you elaborate a little more or possibly provide an example. I have tried using the inner join and I get an empty recordset.
    PARAMETERS prmDate Value;
    SELECT Select_Data.*
    FROM Select_Data
    INNER JOIN Select_Rail_Totals on Select_Data.Rail=Select_Rail_Totals.Rail;

    If i try to use an OUTER join it keeps returning and error.

    Thanks,


  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Can you attach a db here with some sample data?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    theghost is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jan 2018
    Posts
    4
    Scale Data.mdb

    I have attached the database. I am using the Select_Data_Total. This is the one that gives me duplicate data.

    Thanks,

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    I changed the totals query so that it returned the rail field, and then this seems to work:

    SELECT Select_Data.*, Select_Rail_Totals.['Total']
    FROM Select_Data INNER JOIN Select_Rail_Totals ON Select_Data.Rail = Select_Rail_Totals.Rail;

    Date and Time aren't good choices for field names, as they are functions. More here:

    http://allenbrowne.com/AppIssueBadWord.html
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    theghost is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jan 2018
    Posts
    4
    Thank you very much for your help. The comment about the field names is understood.

    This solved my issue. Have a nice day.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    No problem, and welcome to the site by the way!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 4
    Last Post: 11-29-2017, 10:01 AM
  2. Replies: 1
    Last Post: 02-19-2016, 08:16 AM
  3. Replies: 8
    Last Post: 07-29-2014, 12:35 PM
  4. Replies: 4
    Last Post: 09-04-2013, 06:40 AM
  5. Replies: 1
    Last Post: 03-14-2011, 10:04 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