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. Best practice?

Best practice?

Scheduled Pinned Locked Moved Database
databasesalesquestiondiscussion
3 Posts 2 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.
  • E Offline
    E Offline
    econner
    wrote on last edited by
    #1

    With the following table I am looking for suggestions on the best way to query the table for the follow criteria. ID SalesID FKey FValue 1 1 12345 1.25 2 1 555 2.25 3 1 775 3.00 4 2 432 2.00 5 2 12345 1.00 6 3 321 1.11 7 3 223 1.00 8 4 12345 1.25 9 4 775 3.00 10 4 222 1.00 I need to query the table for the SalesID number for the sales that sold both '12345' and '775'. This would be only SalesIDs 1 and 4. Querying FKey = '12345' OR FKey = '775' would return 1,2, and 4 and not just 1 and 4.

    K 1 Reply Last reply
    0
    • E econner

      With the following table I am looking for suggestions on the best way to query the table for the follow criteria. ID SalesID FKey FValue 1 1 12345 1.25 2 1 555 2.25 3 1 775 3.00 4 2 432 2.00 5 2 12345 1.00 6 3 321 1.11 7 3 223 1.00 8 4 12345 1.25 9 4 775 3.00 10 4 222 1.00 I need to query the table for the SalesID number for the sales that sold both '12345' and '775'. This would be only SalesIDs 1 and 4. Querying FKey = '12345' OR FKey = '775' would return 1,2, and 4 and not just 1 and 4.

      K Offline
      K Offline
      Krish KP
      wrote on last edited by
      #2

      Can be done only using corelated subqueries

      SELECT Distinct SalesID FROM fky a WHERE
      EXISTS (SELECT * FROM fky b WHERE a.SalesID = b.SalesID AND fkey = 12345)
      AND
      EXISTS (SELECT * FROM fky c WHERE a.SalesID = c.SalesID AND fkey = 775)

      Regards KP

      E 1 Reply Last reply
      0
      • K Krish KP

        Can be done only using corelated subqueries

        SELECT Distinct SalesID FROM fky a WHERE
        EXISTS (SELECT * FROM fky b WHERE a.SalesID = b.SalesID AND fkey = 12345)
        AND
        EXISTS (SELECT * FROM fky c WHERE a.SalesID = c.SalesID AND fkey = 775)

        Regards KP

        E Offline
        E Offline
        econner
        wrote on last edited by
        #3

        Thanks. This worked great.

        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