X Y DATE
1 20 20120101
1 21 20120101
2 30 20120201
3 40 20120201
3 41 20120301
I want to select any rows that have another row where X is the same, but the date is different, i.e. the answer would be
3 40 20120201
3 41 20120301
You may use the IN, ANY, or ALL operator in outer query to handle a subquery that returns multiple rows. Contents: Using IN operator with a Multiple Row Subquery. Using NOT IN operator with a Multiple Row Subquery.
Try this...
SELECT *
FROM YourTable
WHERE X IN (
SELECT T1.X
FROM YourTable T1 INNER JOIN
YourTable T2 ON T1.X = T2.X
WHERE T1.DATE <> T2.DATE
);
This should work in most ANSI-compliant database products.
select distinct t1.*
from table t1
join table t2
on (t1.X = t2.X and t1.date <> t2.date);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With