X Y DATE
1 20 20120101
1 21 20120101
2 30 20120201
3 40 20120201
3 41 20120301
我想选择任何行,其ä¸X是相åŒçš„å¦ä¸€è¡Œï¼Œä½†æ—¥æœŸä¸åŒï¼Œå³ç”案是
3 40 20120201
3 41 20120301
ç”案 0 :(得分:22)
试试这个......
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
);
这应该适用于大多数符åˆANSIæ ‡å‡†çš„æ•°æ®åº“产å“。
ç”案 1 :(得分:2)
select distinct t1.*
from table t1
join table t2
on (t1.X = t2.X and t1.date <> t2.date);
ç”案 2 :(得分:0)
declare @t table(X int, Y int, DATE CHAR(8))
insert @t values
(1, 20, '20120101' ),
(1, 21, '20120101' ),
(2, 30, '20120201'),
(3, 40, '20120201'),
(3, 41, '20120301')
select x,y, date, maxy, miny from
(
select *, max(date) over (partition by x) maxdate,
min(date) over (partition by x) mindate
from @t
) a
where mindate <> maxdate