日期:2014-05-17 浏览次数:20546 次
Create table t2([X] int)
Insert t2
select '2' union all
select '2' union all
select '1' union all
select '1' union all
select '8'
Create table t1([C1] char(4),[C2] int)
Insert t1
select '001','3' union all
select '002','4' union all
select '001','8' union all
select '003','7' union all
select '002','5'
select C1 from t1 group by C1 having exists(select * from t2 where t2.X>max(t1.C2))
--Create table t2([X] int)
--Insert t2
--select '2' union all
--select '2' union all
--select '1' union all
--select '1' union all
--select '8'
--Create table t1([C1] char(4),[C2] int)
--Insert t1
--select '001','3' union all
--select '002','4' union all
--select '001','8' union all
--select '003','7' union all
--select '002','5'
SELECT C1
FROM t1
GROUP BY C1
HAVING EXISTS ( SELECT *
FROM t2
WHERE t2.X > MAX(t1.C2) )
SELECT a.c1
FROM
(SELECT c1,MAX(c2)c2
FROM t1
GROUP BY c1) a INNER JOIN t2 b ON a.c2<b.x
/*
C1
----
002
003
(2 行受影响)
c1
----
002
003
(2 行受影响)
*/