Results 1 to 5 of 5
  1. #1
    cherold is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    82

    How do I get this query to select rows with no repeats of one value

    I'm trying to figure out how to avoid duplicate rows, or more accurately avoid rows that duplicate one particular value.



    I have two tables, Discs and DiscContents.

    DiscContents is made up of:
    disk_contents_id
    composition_id
    disk_id

    Discs has a DiscID that joins DiscContents at disk_id, and a slew of text fields.

    Right now I'm using this query, which gives me, as I'll explain below, more rows than I want:

    SELECT Discs.*, DiscContents.*
    FROM DiscContents INNER JOIN Discs ON DiscContents.disc_id = Discs.DiscID;

    The issue is that DiscContents.disk_id has records that use the same number, and what I need is a query that will allow me to only grab one record with each disk_id number. So if my query results in:

    disk_contents_id composition_id disk_id DiscID miscellaneous_text_fields
    1 7 2 2 this
    2 4 2 2 that
    3 10 2 2 the
    4 17 9 9 other
    5 3 9 9 thing


    I would want to only see two rows, one for disk_id 2 and one for disk_id 27 (doesn't matter which rows).

    How would I do that?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    disk_id 27 is not in the example data.

    If data from other fields is not important then why reference them?

    SELECT DISTINCT disk_id FROM tablename;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Looks like you couldn't figure out if you wanted to spell it "disc" or "disk" (you have a lot of inconsistent back-and-forth in your post!).

    You can do an Aggregate Query where you group on the DiscID fields and just take the First of all the other fields.
    Code:
    SELECT 
        First(DiscContents.disc_contents_id) AS FirstOfdisc_contents_id, 
        First(DiscContents.composition_id) AS FirstOfcomposition_id, 
        DiscContents.disc_id, 
        Discs.DiscID, 
        First(Discs.miscellaneous_text_fields) AS FirstOfmiscellaneous_text_fields
    FROM DiscContents 
    INNER JOIN Discs ON DiscContents.disc_id = Discs.DiscID
    GROUP BY 
        DiscContents.disc_id, 
        Discs.DiscID;
    Note that you CANNOT use "*" on Aggregate Queries, you need to list out every field you want to display.

  4. #4
    cherold is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    82
    Yeah, I used to always use disk but it seems most people use disc nowadays so I've been trying to alter the habit. It will take a while.

    Thanks.

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I was just pointing that out in case you tried to use my SQL code "as-is" and found it didn't work because of spelling discrepancies.

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

Similar Threads

  1. VBA Select All Rows in a MultiSelect ListBox
    By ItsMe in forum Code Repository
    Replies: 0
    Last Post: 04-09-2014, 01:52 PM
  2. Select Rows From Different Reports
    By yaviens in forum Programming
    Replies: 1
    Last Post: 04-09-2014, 08:59 AM
  3. Getting multiple rows into one row in select
    By rbevers in forum Queries
    Replies: 3
    Last Post: 08-30-2013, 01:58 PM
  4. Multi select rows in recordset
    By Charlie in forum Programming
    Replies: 1
    Last Post: 01-31-2013, 11:49 PM
  5. Replies: 0
    Last Post: 11-30-2010, 12:51 PM

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