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. Getting unique combination of rows in SQL??

Getting unique combination of rows in SQL??

Scheduled Pinned Locked Moved Database
questiondatabasehelp
7 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.
  • L Offline
    L Offline
    Lima3
    wrote on last edited by
    #1

    I have a requirement as in below

    DECLARE @TEST TABLE (X VARCHAR(10),Y VARCHAR(10))

    insert into @TEST values ('A','B'),('C','D'),('E','F'),('B','A'),('D','C'),('F','E')
    

    SELECT A.* FROM @TEST A, @TEST B WHERE A.X = B.y gives X Y A B C D E F B A D C F E But I have to get only the combination (AB or BA) ,(CD or DC),(EF or FE) in my select statement. In my sql we have some thing called DistinctRow.. What is it in SQL ..Pelase help ..urgent..

    A B 2 Replies Last reply
    0
    • L Lima3

      I have a requirement as in below

      DECLARE @TEST TABLE (X VARCHAR(10),Y VARCHAR(10))

      insert into @TEST values ('A','B'),('C','D'),('E','F'),('B','A'),('D','C'),('F','E')
      

      SELECT A.* FROM @TEST A, @TEST B WHERE A.X = B.y gives X Y A B C D E F B A D C F E But I have to get only the combination (AB or BA) ,(CD or DC),(EF or FE) in my select statement. In my sql we have some thing called DistinctRow.. What is it in SQL ..Pelase help ..urgent..

      A Offline
      A Offline
      AHSAN111
      wrote on last edited by
      #2

      Try using the Distinct Keyword

      SELECT Distinct(A.x), A.Y FROM @TEST A, @TEST B WHERE A.X = B.y

      1 Reply Last reply
      0
      • L Lima3

        I have a requirement as in below

        DECLARE @TEST TABLE (X VARCHAR(10),Y VARCHAR(10))

        insert into @TEST values ('A','B'),('C','D'),('E','F'),('B','A'),('D','C'),('F','E')
        

        SELECT A.* FROM @TEST A, @TEST B WHERE A.X = B.y gives X Y A B C D E F B A D C F E But I have to get only the combination (AB or BA) ,(CD or DC),(EF or FE) in my select statement. In my sql we have some thing called DistinctRow.. What is it in SQL ..Pelase help ..urgent..

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

        here it is

        DECLARE @TEST TABLE (X VARCHAR(10),Y VARCHAR(10))

        insert into @TEST values ('A','B')
        insert into @TEST values ('C','D')
        insert into @TEST values ('E','F')
        insert into @TEST values ('B','A')
        insert into @TEST values ('D','C')
        insert into @TEST values ('F','E')

        SELECT A.X
        ,(
        select top 1 b.y from @test as b where b.y > a.x and b.x a.x and b.x


        I Love T-SQL
        "VB.NET is developed with C#.NET"
        If my post helps you kindly save my time by voting my post.

        L 1 Reply Last reply
        0
        • B Blue_Boy

          here it is

          DECLARE @TEST TABLE (X VARCHAR(10),Y VARCHAR(10))

          insert into @TEST values ('A','B')
          insert into @TEST values ('C','D')
          insert into @TEST values ('E','F')
          insert into @TEST values ('B','A')
          insert into @TEST values ('D','C')
          insert into @TEST values ('F','E')

          SELECT A.X
          ,(
          select top 1 b.y from @test as b where b.y > a.x and b.x a.x and b.x


          I Love T-SQL
          "VB.NET is developed with C#.NET"
          If my post helps you kindly save my time by voting my post.

          L Offline
          L Offline
          Lima3
          wrote on last edited by
          #4

          Excellent .. thanks for the reply..

          L 1 Reply Last reply
          0
          • L Lima3

            Excellent .. thanks for the reply..

            L Offline
            L Offline
            Lima3
            wrote on last edited by
            #5

            I was looking for some key word like distinctrow in MYSQL.. dont we have any thing like that in SQL.

            L B 2 Replies Last reply
            0
            • L Lima3

              I was looking for some key word like distinctrow in MYSQL.. dont we have any thing like that in SQL.

              L Offline
              L Offline
              Lima3
              wrote on last edited by
              #6

              Finally SELECT X,Y FROM @TEST WHERE X

              1 Reply Last reply
              0
              • L Lima3

                I was looking for some key word like distinctrow in MYSQL.. dont we have any thing like that in SQL.

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

                There exists DISTINCT option to retrieve non-repeated values, but your case was different from using DISTINCT. Glad to help you.


                I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

                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