Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. sql novice: how to find only duplicates

sql novice: how to find only duplicates

Scheduled Pinned Locked Moved Database
questiondatabasetutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • PJ ArendsP Offline
    PJ ArendsP Offline
    PJ Arends
    wrote on last edited by
    #1

    SELECT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;

    6582 row(s) affected

    SELECT DISTINCT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;

    6473 rows(s) affected How can I get just the 109 rows that are duplicates? ie: the opposite of DISTINCT? Thanks


    You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

    Within you lies the power for good; Use it!

    B J M 3 Replies Last reply
    0
    • PJ ArendsP PJ Arends

      SELECT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;

      6582 row(s) affected

      SELECT DISTINCT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;

      6473 rows(s) affected How can I get just the 109 rows that are duplicates? ie: the opposite of DISTINCT? Thanks


      You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      Try to union all by three selects like:

      select distinct col1 from mytable
      union all
      select distinct col2 from mytable
      union all
      select distinct col3 from mytable


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

      PJ ArendsP 1 Reply Last reply
      0
      • PJ ArendsP PJ Arends

        SELECT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;

        6582 row(s) affected

        SELECT DISTINCT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;

        6473 rows(s) affected How can I get just the 109 rows that are duplicates? ie: the opposite of DISTINCT? Thanks


        You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        You can do this using a Group By clause

        SELECT Col1,Col2,Col3, COUNT(*) as DuplicateCount
        FROM MyTable
        GROUP BY Col1,Col2,Col3
        ORDER BY DuplicateCount DESC

        At this point you will have all the records, with the duplicates (Those with a count>1) at the beginning of the list. Interestingly, you can reduce this to JUST the duplicates by replacing the ORDER BY with a HAVING clause

        SELECT Col1,Col2,Col3, COUNT(*) as DuplicateCount
        FROM MyTable
        GROUP BY Col1,Col2,Col3
        HAVING COUNT(*)>1

        Now you only have the duplicates.

        PJ ArendsP 1 Reply Last reply
        0
        • PJ ArendsP PJ Arends

          SELECT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;

          6582 row(s) affected

          SELECT DISTINCT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;

          6473 rows(s) affected How can I get just the 109 rows that are duplicates? ie: the opposite of DISTINCT? Thanks


          You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          You're next question will be how do I remove the duplicates? Look into row_number() and partition over in BOL

          Never underestimate the power of human stupidity RAH

          PJ ArendsP 1 Reply Last reply
          0
          • B Blue_Boy

            Try to union all by three selects like:

            select distinct col1 from mytable
            union all
            select distinct col2 from mytable
            union all
            select distinct col3 from mytable


            I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

            PJ ArendsP Offline
            PJ ArendsP Offline
            PJ Arends
            wrote on last edited by
            #5

            Thanks for the help, but unfortunately this does not give me the results I am looking for.


            You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

            Within you lies the power for good; Use it!

            1 Reply Last reply
            0
            • J J4amieC

              You can do this using a Group By clause

              SELECT Col1,Col2,Col3, COUNT(*) as DuplicateCount
              FROM MyTable
              GROUP BY Col1,Col2,Col3
              ORDER BY DuplicateCount DESC

              At this point you will have all the records, with the duplicates (Those with a count>1) at the beginning of the list. Interestingly, you can reduce this to JUST the duplicates by replacing the ORDER BY with a HAVING clause

              SELECT Col1,Col2,Col3, COUNT(*) as DuplicateCount
              FROM MyTable
              GROUP BY Col1,Col2,Col3
              HAVING COUNT(*)>1

              Now you only have the duplicates.

              PJ ArendsP Offline
              PJ ArendsP Offline
              PJ Arends
              wrote on last edited by
              #6

              Thanks, this is exactly what I am looking for.


              You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

              Within you lies the power for good; Use it!

              1 Reply Last reply
              0
              • M Mycroft Holmes

                You're next question will be how do I remove the duplicates? Look into row_number() and partition over in BOL

                Never underestimate the power of human stupidity RAH

                PJ ArendsP Offline
                PJ ArendsP Offline
                PJ Arends
                wrote on last edited by
                #7

                Actually not. Using the results I got using J4amieC's answer I am going to try and figure out which other column in the table will make these rows unique.


                You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

                Within you lies the power for good; Use it!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups