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. General Programming
  3. Visual Basic
  4. Tricky SQL Question

Tricky SQL Question

Scheduled Pinned Locked Moved Visual Basic
databasequestiontutorial
12 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.
  • T ttocsmi

    I have an Access database for which I'm trying to write a query based on a request from the users. I have a table of data related to artwork, with three columns: ArtworkId, DescriptionText, and CalloutNumber. Each piece of artwork can have more than one DescriptionText value. What would be a good way to write an SQL query to return, for example, all ArtworkId records having both DescriptionText = "Start Switch" and DescriptionText = "Stop Switch" records? ArtworkID / DescriptionText 123 / Motor 234 / Fuse 234 / Start Switch 456 / Stop Switch 789 / Start Switch 789 / Stop Switch 789 / Light Result = ArtworkID 789 Any assistance will be greatly appreciated. Thanks!

    M Offline
    M Offline
    Martijn Kok
    wrote on last edited by
    #2

    I think the follow query would do what you want:

    SELECT ArtworkID
    FROM Artwork
    WHERE DescriptionText In ("Start Switch","Stop Switch")
    GROUP BY ArtworkID
    HAVING COUNT(*)=2;

    T B 3 Replies Last reply
    0
    • M Martijn Kok

      I think the follow query would do what you want:

      SELECT ArtworkID
      FROM Artwork
      WHERE DescriptionText In ("Start Switch","Stop Switch")
      GROUP BY ArtworkID
      HAVING COUNT(*)=2;

      T Offline
      T Offline
      ttocsmi
      wrote on last edited by
      #3

      Thanks. I tried something similar this afternoon, but it didn't work. I'll try your idea tomorrow.

      1 Reply Last reply
      0
      • M Martijn Kok

        I think the follow query would do what you want:

        SELECT ArtworkID
        FROM Artwork
        WHERE DescriptionText In ("Start Switch","Stop Switch")
        GROUP BY ArtworkID
        HAVING COUNT(*)=2;

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #4

        Just happens to work with the example data given. It will show wrong results with duplicated entries, e.g. having two times 357,"Start Switch".

        M 1 Reply Last reply
        0
        • T ttocsmi

          I have an Access database for which I'm trying to write a query based on a request from the users. I have a table of data related to artwork, with three columns: ArtworkId, DescriptionText, and CalloutNumber. Each piece of artwork can have more than one DescriptionText value. What would be a good way to write an SQL query to return, for example, all ArtworkId records having both DescriptionText = "Start Switch" and DescriptionText = "Stop Switch" records? ArtworkID / DescriptionText 123 / Motor 234 / Fuse 234 / Start Switch 456 / Stop Switch 789 / Start Switch 789 / Stop Switch 789 / Light Result = ArtworkID 789 Any assistance will be greatly appreciated. Thanks!

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #5

          You must join the table with itself. Something like:

          SELECT a.ArtworkID
          FROM Artwork a
          INNER JOIN Artwork b
          ON a.ArtworkID=b.ArtworkID
          WHERE a.DesciptionText="Start Switch" AND b.DescriptionText="Stop Switch"

          T 1 Reply Last reply
          0
          • B Bernhard Hiller

            Just happens to work with the example data given. It will show wrong results with duplicated entries, e.g. having two times 357,"Start Switch".

            M Offline
            M Offline
            Martijn Kok
            wrote on last edited by
            #6

            You are right. If for some reason you can't prevent duplicate entries, then you can use the following query:

            SELECT ArtworkID
            FROM (SELECT DISTINCT ArtworkID, DescriptionText FROM Artwork) as t1
            WHERE DescriptionText In ("Start Switch","Stop Switch")
            GROUP BY ArtworkID
            HAVING COUNT(*)=2;

            It uses a sub-query to get the distinct values first. The rest functions just the same.

            T 1 Reply Last reply
            0
            • M Martijn Kok

              I think the follow query would do what you want:

              SELECT ArtworkID
              FROM Artwork
              WHERE DescriptionText In ("Start Switch","Stop Switch")
              GROUP BY ArtworkID
              HAVING COUNT(*)=2;

              T Offline
              T Offline
              ttocsmi
              wrote on last edited by
              #7

              For reasons unknown, this didn't work. My intention was for the search text entries to more resemble wildcard text. For example, return all IDs with "switch" or "cable" or whatever in the DescriptionText field. Thanks for your response.

              J 1 Reply Last reply
              0
              • M Martijn Kok

                You are right. If for some reason you can't prevent duplicate entries, then you can use the following query:

                SELECT ArtworkID
                FROM (SELECT DISTINCT ArtworkID, DescriptionText FROM Artwork) as t1
                WHERE DescriptionText In ("Start Switch","Stop Switch")
                GROUP BY ArtworkID
                HAVING COUNT(*)=2;

                It uses a sub-query to get the distinct values first. The rest functions just the same.

                T Offline
                T Offline
                ttocsmi
                wrote on last edited by
                #8

                Nope. Still nothing....

                1 Reply Last reply
                0
                • B Bernhard Hiller

                  You must join the table with itself. Something like:

                  SELECT a.ArtworkID
                  FROM Artwork a
                  INNER JOIN Artwork b
                  ON a.ArtworkID=b.ArtworkID
                  WHERE a.DesciptionText="Start Switch" AND b.DescriptionText="Stop Switch"

                  T Offline
                  T Offline
                  ttocsmi
                  wrote on last edited by
                  #9

                  Well, that worked. I used 'like' for '=' and surrounded the search text with '%' characters. Now, suppose I wanted to search using up to 5 terms - would I have to nest the inner joins? This seems really complicated for something I can see with my eyes & describe easily using English words. (Also, I tried using the Filter By Form command, but it wouldn't work with my form. Thanks for the feedback.

                  1 Reply Last reply
                  0
                  • T ttocsmi

                    I have an Access database for which I'm trying to write a query based on a request from the users. I have a table of data related to artwork, with three columns: ArtworkId, DescriptionText, and CalloutNumber. Each piece of artwork can have more than one DescriptionText value. What would be a good way to write an SQL query to return, for example, all ArtworkId records having both DescriptionText = "Start Switch" and DescriptionText = "Stop Switch" records? ArtworkID / DescriptionText 123 / Motor 234 / Fuse 234 / Start Switch 456 / Stop Switch 789 / Start Switch 789 / Stop Switch 789 / Light Result = ArtworkID 789 Any assistance will be greatly appreciated. Thanks!

                    T Offline
                    T Offline
                    ttocsmi
                    wrote on last edited by
                    #10

                    Surely there's an easier way, but this is how I got it to work (I've updated the table and field names):

                    SELECT *
                    FROM art_callout_table
                    WHERE artboardnumber IN (
                    SELECT artboardnumber FROM art_callout_table
                    WHERE artboardnumber IN (
                    SELECT artboardnumber FROM art_callout_table
                    WHERE lrutext LIKE "%start%")
                    AND lrutext LIKE "%light%")
                    AND lrutext LIKE "%switch%";

                    What confused me repeatedly during development, and what confused the users (they were expecting the search to work one way & I was assuming it to work another), is that there are two distinct search methodologies: 1) Using the built-in Access table filtering commands (or a basic SELECT FROM WHERE statement) to narrow down a table of description text values. Repeated filters can be applied to narrow the list further, but if you're looking for "switch" and "start", all you'll get is individual description text records having both values, eg "big red starting switch" or "switch start light" but not "green switch". Not terribly helpful. 2) Use a different search technique to return all ArtworkID values which have "switch" or "start" or "light" strings within their DescriptionText fields, eg don't look just for DescriptionText records similar to "switch start light blinker". I thought this was going to be much easier than it turned out to be. Thanks all for your help.

                    M 1 Reply Last reply
                    0
                    • T ttocsmi

                      Surely there's an easier way, but this is how I got it to work (I've updated the table and field names):

                      SELECT *
                      FROM art_callout_table
                      WHERE artboardnumber IN (
                      SELECT artboardnumber FROM art_callout_table
                      WHERE artboardnumber IN (
                      SELECT artboardnumber FROM art_callout_table
                      WHERE lrutext LIKE "%start%")
                      AND lrutext LIKE "%light%")
                      AND lrutext LIKE "%switch%";

                      What confused me repeatedly during development, and what confused the users (they were expecting the search to work one way & I was assuming it to work another), is that there are two distinct search methodologies: 1) Using the built-in Access table filtering commands (or a basic SELECT FROM WHERE statement) to narrow down a table of description text values. Repeated filters can be applied to narrow the list further, but if you're looking for "switch" and "start", all you'll get is individual description text records having both values, eg "big red starting switch" or "switch start light" but not "green switch". Not terribly helpful. 2) Use a different search technique to return all ArtworkID values which have "switch" or "start" or "light" strings within their DescriptionText fields, eg don't look just for DescriptionText records similar to "switch start light blinker". I thought this was going to be much easier than it turned out to be. Thanks all for your help.

                      M Offline
                      M Offline
                      Martijn Kok
                      wrote on last edited by
                      #11

                      Good you solved it! :thumbsup:

                      1 Reply Last reply
                      0
                      • T ttocsmi

                        For reasons unknown, this didn't work. My intention was for the search text entries to more resemble wildcard text. For example, return all IDs with "switch" or "cable" or whatever in the DescriptionText field. Thanks for your response.

                        J Offline
                        J Offline
                        jrumage
                        wrote on last edited by
                        #12

                        I like LIKE Maybe this article will help http://office.microsoft.com/en-us/access-help/like-operator-HP001032253.aspx[^]

                        rum

                        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