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. Duplicate in join

Duplicate in join

Scheduled Pinned Locked Moved Database
databasehelpquestion
7 Posts 3 Posters 1 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.
  • G Offline
    G Offline
    gnjunge
    wrote on last edited by
    #1

    Hi, This must be real simple, but I'm having a blackout. I have two tables: Names:

    ID Name
    1 John
    2 Peter
    3 Carl
    4 Bernard

    Nicknames:

    ID NameID NickName
    1 1 Bonny
    2 1 Bobby
    3 1 Jo
    4 2 Pete
    5 3 Bab
    6 3 Foo

    Now I have a search running, which looks for a certain string in both the nickname field as well as in the name field. But it should return only 1 record per name (doesn't matter which nickname), so if the seachstring would be 'b%' it would return the following.

    NameID NickNameID Name Nickname
    4 null Bernard null
    1 1 John Bonny
    3 5 Bernard Bab

    I fall on the distinct record thing for the nicknames, I have a dirty subselect, but since the query is actually much bigger, and the table contains 100.000 records, it isn't the right solution. Does anybody have the correct solution to this issue?

    W B 2 Replies Last reply
    0
    • G gnjunge

      Hi, This must be real simple, but I'm having a blackout. I have two tables: Names:

      ID Name
      1 John
      2 Peter
      3 Carl
      4 Bernard

      Nicknames:

      ID NameID NickName
      1 1 Bonny
      2 1 Bobby
      3 1 Jo
      4 2 Pete
      5 3 Bab
      6 3 Foo

      Now I have a search running, which looks for a certain string in both the nickname field as well as in the name field. But it should return only 1 record per name (doesn't matter which nickname), so if the seachstring would be 'b%' it would return the following.

      NameID NickNameID Name Nickname
      4 null Bernard null
      1 1 John Bonny
      3 5 Bernard Bab

      I fall on the distinct record thing for the nicknames, I have a dirty subselect, but since the query is actually much bigger, and the table contains 100.000 records, it isn't the right solution. Does anybody have the correct solution to this issue?

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      You say that the select should return only one nickname. In your example, why did the select return Bonny for Bernard, not Bobby. Is there somekind of logic or is it simply random.

      The need to optimize rises from a bad design.My articles[^]

      G 1 Reply Last reply
      0
      • W Wendelius

        You say that the select should return only one nickname. In your example, why did the select return Bonny for Bernard, not Bobby. Is there somekind of logic or is it simply random.

        The need to optimize rises from a bad design.My articles[^]

        G Offline
        G Offline
        gnjunge
        wrote on last edited by
        #3

        Random, I don't care which one he returns.

        W 1 Reply Last reply
        0
        • G gnjunge

          Random, I don't care which one he returns.

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          Okey, then you could use normal outer join between the tables and for example check that there's no greater nicknameid for the same name (using for example correlated not exists clause).

          The need to optimize rises from a bad design.My articles[^]

          1 Reply Last reply
          0
          • G gnjunge

            Hi, This must be real simple, but I'm having a blackout. I have two tables: Names:

            ID Name
            1 John
            2 Peter
            3 Carl
            4 Bernard

            Nicknames:

            ID NameID NickName
            1 1 Bonny
            2 1 Bobby
            3 1 Jo
            4 2 Pete
            5 3 Bab
            6 3 Foo

            Now I have a search running, which looks for a certain string in both the nickname field as well as in the name field. But it should return only 1 record per name (doesn't matter which nickname), so if the seachstring would be 'b%' it would return the following.

            NameID NickNameID Name Nickname
            4 null Bernard null
            1 1 John Bonny
            3 5 Bernard Bab

            I fall on the distinct record thing for the nicknames, I have a dirty subselect, but since the query is actually much bigger, and the table contains 100.000 records, it isn't the right solution. Does anybody have the correct solution to this issue?

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

            gnjunge wrote:

            I have a dirty subselect

            I hope this T-SQL is not "dirty" but I couldn't make shorter,I hope too this T-SQL will help you. select id, (select top 1 A.ID from nicknames as A where A.nameID = names.ID) as NicknameID, name, (select top 1 A.nickname from nicknames as A where A.nameID = names.ID) as Nickname from names where (names.name like 'b%') or names.id in (select C.nameid from nicknames as C where C.nickname like 'b%' ) </code> Result is : `1 1 John Bonny 3 5 Carl Bab 4 NULL Bernard NULL` * * * 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.

            G 1 Reply Last reply
            0
            • B Blue_Boy

              gnjunge wrote:

              I have a dirty subselect

              I hope this T-SQL is not "dirty" but I couldn't make shorter,I hope too this T-SQL will help you. select id, (select top 1 A.ID from nicknames as A where A.nameID = names.ID) as NicknameID, name, (select top 1 A.nickname from nicknames as A where A.nameID = names.ID) as Nickname from names where (names.name like 'b%') or names.id in (select C.nameid from nicknames as C where C.nickname like 'b%' ) </code> Result is : `1 1 John Bonny 3 5 Carl Bab 4 NULL Bernard NULL` * * * 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.

              G Offline
              G Offline
              gnjunge
              wrote on last edited by
              #6

              This seems like it would be very slow, or isn't it? I guess in the end the names table would contain around 10.000 records,whereas the nicknames table would contain around 50.000

              B 1 Reply Last reply
              0
              • G gnjunge

                This seems like it would be very slow, or isn't it? I guess in the end the names table would contain around 10.000 records,whereas the nicknames table would contain around 50.000

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

                gnjunge wrote:

                This seems like it would be very slow, or isn't it?

                Did you try to run in your real data and to see if is it slow? For data which you post,query is not slow.


                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.

                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