Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2004
    Posts
    1

    Unanswered: T-SQL code help.

    I'm fairly new to SQL

    Should be easy for someone

    Example dataset:

    Table1

    ID Code
    1 A
    1 B
    2 A

    Result set I want
    ID Code
    2 A

    so I only want ID's that = A but don't also have B

    I was trying intersects but T-SQL has issues with it.

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    The relational algebra operators aren't implemented in Transact SQL. You want something like:
    PHP Code:
    SELECT *
       
    FROM Table1 AS a
       WHERE  
    'A' a.code
          
    AND NOT EXISTS (SELECT *
             
    FROM Table1 AS b
             WHERE  b
    .id a.id
                
    AND 'B' b.code
    It's kind of funky compared to the simplicity of relational algebra, but it does exactly the same thing at a lower logical level.

    -PatP

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Whoops, wait a second! It is NOT the same thing as INTERSECTS, but it produces the results that you specified. I'll let you work out the relational algebra expression for what it really does (and no, I don't do homework, even though I will help out a bit here and there).

    -PatP

  4. #4
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    select t.[ID], t.[Code]
    from Table1 t
    inner join (
    select [ID], cnt=count(*)
    from Table1
    group by [ID]
    having count(*)=1) x
    on t.[ID] = x.[ID]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •