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. Retrieve duplicate and non duplicate query

Retrieve duplicate and non duplicate query

Scheduled Pinned Locked Moved Database
databasecareer
8 Posts 3 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.
  • S Offline
    S Offline
    sujtha
    wrote on last edited by
    #1

    Hi, I have employee table. ID empno salary empname 1 1 5000 sdf 2 2 4000 sdf 3 3 4000 sdfas 4 4 4500 sdf 5 5 5000 k 6 8 1000 TT I tried to retrieve the non duplicate rows from the using. Ex: the output should be ID empno salary empname 1 1 5000 sdf 2 2 4000 sdf 4 4 4500 sdf 6 8 1000 TT It should not be display the duplicate values in the salary. Thanks Sujatha

    S B 2 Replies Last reply
    0
    • S sujtha

      Hi, I have employee table. ID empno salary empname 1 1 5000 sdf 2 2 4000 sdf 3 3 4000 sdfas 4 4 4500 sdf 5 5 5000 k 6 8 1000 TT I tried to retrieve the non duplicate rows from the using. Ex: the output should be ID empno salary empname 1 1 5000 sdf 2 2 4000 sdf 4 4 4500 sdf 6 8 1000 TT It should not be display the duplicate values in the salary. Thanks Sujatha

      S Offline
      S Offline
      soni uma
      wrote on last edited by
      #2

      SELECT * FROM tblName WHERE (Id IN (SELECT Id FROM tblName GROUP BY Id HAVING COUNT(Id) = 1)) --> This query gives you a non duplicate rows SELECT * FROM tblName WHERE (Id IN (SELECT Id FROM tblName GROUP BY Id HAVING COUNT(Id) > 1)) --> This query gives you a duplicate rows

      B 1 Reply Last reply
      0
      • S sujtha

        Hi, I have employee table. ID empno salary empname 1 1 5000 sdf 2 2 4000 sdf 3 3 4000 sdfas 4 4 4500 sdf 5 5 5000 k 6 8 1000 TT I tried to retrieve the non duplicate rows from the using. Ex: the output should be ID empno salary empname 1 1 5000 sdf 2 2 4000 sdf 4 4 4500 sdf 6 8 1000 TT It should not be display the duplicate values in the salary. Thanks Sujatha

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

        here it is: SELECT (SELECT TOP 1 t1.id FROM dbo.TableName AS t1 WHERE t1.salary = dbo.TableName.salary) AS id , (SELECT TOP 1 t1.empno FROM dbo.TableName AS t1 WHERE t1.salary = dbo.TableName.salary) AS empno, salary, (SELECT TOP 1 t1.empname FROM dbo.TableName AS t1 WHERE t1.salary = dbo.TableName.salary) AS empname FROM dbo.TableName GROUP BY salary ORDER BY id ASC


        I Love T-SQL "Don't torture yourself,let the life to do it for you."

        S 1 Reply Last reply
        0
        • S soni uma

          SELECT * FROM tblName WHERE (Id IN (SELECT Id FROM tblName GROUP BY Id HAVING COUNT(Id) = 1)) --> This query gives you a non duplicate rows SELECT * FROM tblName WHERE (Id IN (SELECT Id FROM tblName GROUP BY Id HAVING COUNT(Id) > 1)) --> This query gives you a duplicate rows

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

          Your first return all rows, second query return no rows. Regards


          I Love T-SQL "Don't torture yourself,let the life to do it for you."

          1 Reply Last reply
          0
          • B Blue_Boy

            here it is: SELECT (SELECT TOP 1 t1.id FROM dbo.TableName AS t1 WHERE t1.salary = dbo.TableName.salary) AS id , (SELECT TOP 1 t1.empno FROM dbo.TableName AS t1 WHERE t1.salary = dbo.TableName.salary) AS empno, salary, (SELECT TOP 1 t1.empname FROM dbo.TableName AS t1 WHERE t1.salary = dbo.TableName.salary) AS empname FROM dbo.TableName GROUP BY salary ORDER BY id ASC


            I Love T-SQL "Don't torture yourself,let the life to do it for you."

            S Offline
            S Offline
            sujtha
            wrote on last edited by
            #5

            I tried to execute the above query in the msacess. it throws an error msg as you tried to execute a query that does not include the specified expression 'id' as part of an aggregate function Kindly help ASAP.

            B 1 Reply Last reply
            0
            • S sujtha

              I tried to execute the above query in the msacess. it throws an error msg as you tried to execute a query that does not include the specified expression 'id' as part of an aggregate function Kindly help ASAP.

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

              remove line ORDER BY id ASC


              I Love T-SQL "Don't torture yourself,let the life to do it for you."

              S 1 Reply Last reply
              0
              • B Blue_Boy

                remove line ORDER BY id ASC


                I Love T-SQL "Don't torture yourself,let the life to do it for you."

                S Offline
                S Offline
                sujtha
                wrote on last edited by
                #7

                It's working. Thanks a lot.

                B 1 Reply Last reply
                0
                • S sujtha

                  It's working. Thanks a lot.

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

                  No problem.


                  I Love T-SQL "Don't torture yourself,let the life to do it for you."

                  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