Access 2010 query duplicates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jacekwbak
    New Member
    • Feb 2013
    • 4

    Access 2010 query duplicates

    Hello,

    I have following problem:

    table1
    id / ArticleCode / ArticleColor
    1 / 123 / blue
    2 / 123 / blue
    3 / 222 / green
    4 / 333 / red
    5 / 444 / blue

    Out of this data I need to pick unique values to return:

    1 / 123 / blue
    3 / 222 / green
    4 / 333 / red
    5 / 444 / blue

    so basically, i need to keep all the fields, but throw out only records that have duplicate ArticleCode

    When I run a wizard to find duplicates, and include ID and ArticleColor, it returns all recods.

    When I use COUNT([ArticleCode])=1 it returns only record w/o duplicates.


    Please help me out
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    You need a SELECT DISTINCT query.

    Code:
    SELECT *
    FROM table1

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      Respectfully Seth, That will not work, nor will "DISTINCTRO W" in that the field [ID] contains a unique value. You really should try it in a test database, don't take my word for it... ;-)

      Now, if OP does not need [ID] then you can include only the [ArticleCode] and [ArticleColor] in the query use either of the "DISTINCT" Predicates.

      Comment

      • jacekwbak
        New Member
        • Feb 2013
        • 4

        #4
        Thanks for reply,

        Code:
        select distinct *
        from table1

        still will return all the records because of unique ID

        If I only SELECT DISTINCT on ArticleCode then ok, but problem is that I need the ID.

        What Now?
        Last edited by jacekwbak; Feb 8 '13, 02:24 PM.

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          AH, Thank You for attempting the SQL.
          You really should read my post. It would have saved you some frustration.

          I have one solution that I use quite often for things like this... took me ages to figure out and as it is not straight forward try the following:

          I actually have a template table and query set for this:

          tbl_test
          PK is the primary key,
          Code:
          SELECT [test_PK], 
             [Test_Field1], 
             [Test_Field2]
          FROM [tbl_test] AS  t1
          WHERE NOT EXISTS 
             (SELECT 1
              FROM [tbl_test] AS t2
              WHERE [t2]![test_field1] = [t1]![test_field1]
                   AND 
                         [t2]![test_field2] = [t1]![test_field2]
                   AND
                        [t2]![test_pk] <[t1]![test_pk]);
          Now I hope Rabbit or NeoPa have something more elegant :)

          Comment

          • Seth Schrock
            Recognized Expert Specialist
            • Dec 2010
            • 2965

            #6
            Duh. I'm not sure what I was thinking, but I clearly missed the ID field throwing off the DISTINCT query results.

            Comment

            • Narender Sagar
              New Member
              • Jul 2011
              • 189

              #7
              If I have to solve this, I'd simply create following query :
              Code:
              SELECT Table1.COLOR, Table1.CODE, First(Table1.ID) AS FirstOfID
              FROM Table1
              GROUP BY Table1.COLOR, Table1.CODE;
              I tried above query and got desired result..
              Last edited by NeoPa; Feb 10 '13, 11:16 PM. Reason: Tidying layout for Best Answer.

              Comment

              • zmbd
                Recognized Expert Moderator Expert
                • Mar 2012
                • 5501

                #8
                Told you there had to be a better method for your data set... the query I posted is a much shorter one I use against 16 fields... it's an old database that was not designed very well by one of my predecessors; however, given what it does, it's impressive (but slow).
                I never thought to re-work it for smaller datasets...

                Comment

                • jacekwbak
                  New Member
                  • Feb 2013
                  • 4

                  #9
                  Wow. Problem Solved! Thanks

                  Comment

                  • Narender Sagar
                    New Member
                    • Jul 2011
                    • 189

                    #10
                    If you post what solves your problem, it will be beneficial for other readers.
                    thanks.

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32645

                      #11
                      Originally posted by Narender
                      Narender:
                      If you post what solves your problem, it will be beneficial for other readers.
                      I suspect you misunderstand. My understanding from reading through the thread is that your post (which I've now selected as Best Answer) was exactly the breakthrough they were looking for.

                      @Jacejwbak.
                      I hope you noted that many experts were confused by your question asking for one thing, then indicating with the example something quite different. Seth actually answered the question perfectly, but that wasn't what you needed because the question was asked wrong. Typically, the clearer the question is, the quicker and more easily you will find an answer posted.

                      Comment

                      Working...