27,579
社区成员
发帖
与我相关
我的任务
分享
declare @s table(a int,b int,c int)
insert into @s select 11,2,1
union all select 11,1,0
union all select 22,1,1
union all select 22,1,0
union all select 33,1,1
union all select 33,2,2
--select a from @s j where b=(select b from @s where j.a=a and j.b<b)
--select * from @s
select * from @s j where not exists(select 1 from @s where a=j.a and b>j.b)
and c=0
DECLARE @TB TABLE(a INT,b INT,c INT)
INSERT @TB
SELECT 11,2,1 UNION ALL
SELECT 11,1,0 UNION ALL
SELECT 22,1,1 UNION ALL
SELECT 22,1,0 UNION ALL
SELECT 33,1,1 UNION ALL
SELECT 33,2,2
select * from @TB BB where not exists (select * from @TB where a=BB.a and b>BB.b) and c=0
SELECT a FROM s t where b=(SELECT max(b) FROM s where a=t.a) and c=0
DECLARE @TB TABLE(a INT, b INT, c INT)
INSERT @TB
SELECT 11, 2, 1 UNION ALL
SELECT 11, 1, 0 UNION ALL
SELECT 22, 1, 1 UNION ALL
SELECT 22, 1, 0 UNION ALL
SELECT 33, 1, 1 UNION ALL
SELECT 33, 2, 2
SELECT A,MAX(B) AS B FROM @TB WHERE C=0 GROUP BY A
/*
A B
----------- -----------
11 1
22 1
*/