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. Query Scenario

Query Scenario

Scheduled Pinned Locked Moved Database
databasehelptutorial
3 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.
  • R Offline
    R Offline
    RadioButton
    wrote on last edited by
    #1

    Hello, I need some help with a SQL query. Example table: Table myTable Columns myId, myDate If I have rows in the table with the same id like this: myid mydate ---- -------- 1 01/01/1999 1 blank 1 01/01/1999 2 01/02/1999 I would like to filter out the id completely if I find the id has a record with a date and a record with a blank date. If the filter works my results should be: myid mydate ---- -------- 2 01/02/1999 Id 2 remains because it does not have records with a blank date. Thank you so much for the help.

    M 1 Reply Last reply
    0
    • R RadioButton

      Hello, I need some help with a SQL query. Example table: Table myTable Columns myId, myDate If I have rows in the table with the same id like this: myid mydate ---- -------- 1 01/01/1999 1 blank 1 01/01/1999 2 01/02/1999 I would like to filter out the id completely if I find the id has a record with a date and a record with a blank date. If the filter works my results should be: myid mydate ---- -------- 2 01/02/1999 Id 2 remains because it does not have records with a blank date. Thank you so much for the help.

      M Offline
      M Offline
      Michael Potter
      wrote on last edited by
      #2

      Your request is somewhat confusing but, I think I grasped the basic concept. I assume you are using blank as a term that represents NULL.

      SELECT 
          myid, 
          mydate
      FROM 
          myTable
      WHERE 
          myid NOT IN (SELECT 
                           myid
                       FROM 
                           myTable
                       WHERE 
                           mydate IS NULL)
      
      A 1 Reply Last reply
      0
      • M Michael Potter

        Your request is somewhat confusing but, I think I grasped the basic concept. I assume you are using blank as a term that represents NULL.

        SELECT 
            myid, 
            mydate
        FROM 
            myTable
        WHERE 
            myid NOT IN (SELECT 
                             myid
                         FROM 
                             myTable
                         WHERE 
                             mydate IS NULL)
        
        A Offline
        A Offline
        Ashfield
        wrote on last edited by
        #3

        Try SELECT myid, mydate FROM myTable mt1 WHERE not exists (select 1 from myTable mt2 where mt2.myID = mt1.myid and mt2.mydate is null) Assuming the table is properly indexed this should execute quicker as a not exists is quicker than a not in.

        Bob Ashfield Consultants Ltd

        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