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. Another IN clause Question

Another IN clause Question

Scheduled Pinned Locked Moved Database
questiondatabase
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.
  • M Offline
    M Offline
    MaxRelaxman
    wrote on last edited by
    #1

    I'm having some issues with a Query I'm trying to use to fine similar records in a master table. There are several child tables that will be searched in a similar way. Basically, if there are more than two matches in the sub query I want to display those records. Can anyone point me in the right direction to get this query to work? I understand why I can't use more than one field in my subquery but I can't think of another way to do this.

    SELECT *
    FROM UC
    WHERE uc_key IN (SELECT mo_Key, COUNT(mo_key) as KeyCount
    FROM MO
    WHERE (mo_MoKey = @Key1 AND mo_Value = @Value1) OR (mo_MoKey = @Key2 AND mo_Value = @Value2) <- there will be a variable number of these that get generated programatically
    AND KeyCount > 2)

    (note, the table names have been mangled to protect the innocent) Thanks, any ideas are appreciated.

    P W 2 Replies Last reply
    0
    • M MaxRelaxman

      I'm having some issues with a Query I'm trying to use to fine similar records in a master table. There are several child tables that will be searched in a similar way. Basically, if there are more than two matches in the sub query I want to display those records. Can anyone point me in the right direction to get this query to work? I understand why I can't use more than one field in my subquery but I can't think of another way to do this.

      SELECT *
      FROM UC
      WHERE uc_key IN (SELECT mo_Key, COUNT(mo_key) as KeyCount
      FROM MO
      WHERE (mo_MoKey = @Key1 AND mo_Value = @Value1) OR (mo_MoKey = @Key2 AND mo_Value = @Value2) <- there will be a variable number of these that get generated programatically
      AND KeyCount > 2)

      (note, the table names have been mangled to protect the innocent) Thanks, any ideas are appreciated.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      You probably still want a JOIN; avoid using IN for anything other than literal lists.

      M 1 Reply Last reply
      0
      • M MaxRelaxman

        I'm having some issues with a Query I'm trying to use to fine similar records in a master table. There are several child tables that will be searched in a similar way. Basically, if there are more than two matches in the sub query I want to display those records. Can anyone point me in the right direction to get this query to work? I understand why I can't use more than one field in my subquery but I can't think of another way to do this.

        SELECT *
        FROM UC
        WHERE uc_key IN (SELECT mo_Key, COUNT(mo_key) as KeyCount
        FROM MO
        WHERE (mo_MoKey = @Key1 AND mo_Value = @Value1) OR (mo_MoKey = @Key2 AND mo_Value = @Value2) <- there will be a variable number of these that get generated programatically
        AND KeyCount > 2)

        (note, the table names have been mangled to protect the innocent) Thanks, any ideas are appreciated.

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

        Hi, Didn't test this so there may be typos but try something like this:

        SELECT *
        FROM UC
        WHERE uc_key IN (SELECT mo_Key
        FROM MO
        WHERE (mo_MoKey = @Key1 AND mo_Value = @Value1)
        OR (mo_MoKey = @Key2 AND mo_Value = @Value2)
        GROUP BY mo_Key
        HAVING COUNT(*) > 2);

        Hope this helps, Mika

        M 1 Reply Last reply
        0
        • P PIEBALDconsult

          You probably still want a JOIN; avoid using IN for anything other than literal lists.

          M Offline
          M Offline
          MaxRelaxman
          wrote on last edited by
          #4

          Using a JOIN will improve speed?

          P 1 Reply Last reply
          0
          • W Wendelius

            Hi, Didn't test this so there may be typos but try something like this:

            SELECT *
            FROM UC
            WHERE uc_key IN (SELECT mo_Key
            FROM MO
            WHERE (mo_MoKey = @Key1 AND mo_Value = @Value1)
            OR (mo_MoKey = @Key2 AND mo_Value = @Value2)
            GROUP BY mo_Key
            HAVING COUNT(*) > 2);

            Hope this helps, Mika

            M Offline
            M Offline
            MaxRelaxman
            wrote on last edited by
            #5

            Excellent, thank you for your help! One thousand internet dollars are coming your way, just put your ethernet cable into the trash can can to catch it all.

            1 Reply Last reply
            0
            • M MaxRelaxman

              Using a JOIN will improve speed?

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              It has in my experience. And much more flexible.

              M 1 Reply Last reply
              0
              • P PIEBALDconsult

                It has in my experience. And much more flexible.

                M Offline
                M Offline
                MaxRelaxman
                wrote on last edited by
                #7

                I'll have to experiment in that direction then. This is a nasty complicated app and I'll need all the little speed boosts I can get. Thanks!

                P 1 Reply Last reply
                0
                • M MaxRelaxman

                  I'll have to experiment in that direction then. This is a nasty complicated app and I'll need all the little speed boosts I can get. Thanks!

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  I can imagine. Your first task should be to eliminate the subqueries; they don't benefit from indices. I had to change some of my JOINs the other day because the statements seemed to just sit there, now they zip along.

                  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