Results 1 to 3 of 3

Thread: Query Help

  1. #1
    Join Date
    Dec 2010
    Posts
    2

    Unanswered: Query Help

    Hey all,

    *Using SQL Server 2005*

    Here's an example of what I'm trying to get at:

    I have 2 tables, one called Table1 and the other called Table2.

    Data in Table1:
    Code Status Name
    T123 Pending ABCD
    T234 Active EFGH
    T345 Active IJKL
    T456 Active MNOP
    T567 Pending QRST

    Data in Table2: (each code can have multiple type values)
    Code Type
    T123 S
    T123 N
    T234 S
    T345 S
    T345 N
    T456 S
    T456 N
    T567 S

    What I want to get are the Codes that are only Type S, not the Codes that are both Type S and Type N and of course, display the Status and Name for the Code.

    Example Result I want to see from the example table data:
    Code Status Name Type
    T234 Active EFGH S
    T567 Pending QRST S

    Any assistance would be greatly appreciated.
    Thanks!

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    there are oh so many ways to achieve the results you want

    this is but one of them
    Code:
    SELECT Code 
         , Status 
         , Name
         , 'S' AS type
      FROM table1
     WHERE EXISTS 
           ( SELECT 'humpty' 
               FROM table2
              WHERE code = table1.code
                AND type = 'S' )  
       AND NOT EXISTS 
           ( SELECT 'dumpty' 
               FROM table2
              WHERE code = table1.code
                AND type = 'N' )
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Dec 2010
    Posts
    2
    That worked perfect...thanks a ton!

Posting Permissions

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