SQL to return selected rows

  • Need SQL to return all rows where C1 occurs more than once, C1 is duplicated, AND, C2 contains different values.

    In this example, the AAA rows and CCC rows would be rendered; they meet the above criteria:

    C1    C2

    AAA   123

    AAA   456

    BBB   333

    BBB   333

    CCC   788

    CCC   788

    CCC   891

    DDD   555

    BT
  • SELECT C1,C2
    
    FROM dbo.YourTable AS YT
    
    WHERE C1 IN (
        SELECT C1 
        FROM dbo.YourTable 
        GROUP BY C1 
        HAVING COUNT(*)>1 
           AND MIN(C2)MAX(C2)
    )
    
  • You may need to add some null handling in the having clause.

    Tim Wilkinson

    "If it doesn't work in practice, you're using the wrong theory"
    - Immanuel Kant

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply