tb
f1 f2 f3 f4
--------------------------
1 x 001 1001
2 x 002 1002
3 x 003 1003
4 w xyz 1003
取的结果 如果是f3最大值
x
--------------
003
取的结果 如果查询f3的 x
w f4
--------------
003 1003
f1 f2 f3 f4
--------------------------
1 x 001 1001
2 x 002 1002
3 x 003 1003
4 w xyz 1003
取的结果 如果是f3最大值
x
--------------
003
取的结果 如果查询f3的 x
w f4
--------------
003 1003
如果是这样的话.SELECT f1,ISNULL(NULLIF(f3,mf3),mf3) f2,f3,f4
FROM tb a
LEFT JOIN (SELECT MAX(f3) mF3 FROM tb) b
ON 1=12,没看明白.楼主给点语言描述可以吗?
1. 相当正常 select max(f3) from tb where f2='x' and f4='1001'
例 我现在找 f2 中条件是x ,取的f3的最大值2.相当正常 select w ,f4 from tb where f2='w' and f4='1001'
1. 相当正常 select max(f3) from tb where f2='x'
tb
f1 f2 f3 f4
--------------------------
1 x 001 1001
2 x 002 1002
3 x 003 1003
4 w xyz 1003
得到结果
x
--------------
003
2.相当正常 select w ,f4 from tb where f2='w' and f4='1001'
得到结果
x
--------------
003
w f4
--------------
003 1003
tb
f1 f2 f3 f4
--------------------------
1 x 001 1001
2 x 002 1002
3 x 003 1003
4 w xyz 1003 我要 取到 F2 为 X 的最大值
(
f1 int,
f2 char(1),
f3 varchar(4),
f4 varchar(10)
)
insert into testtb
select 1,'x','001','1001' union all
select 2,'x','002','1001' union all
select 3,'x','003','1001' union all
select 4,'w','xyz','1001' select * from testtbselect max(f3) as f3 from testtb where f2='x'这样? 我不知道你各个字段的类型
Create Table tb
(
f1 Int,
f2 Char(1),
f3 Varchar(4),
f4 Varchar(10))
Insert Into tb
Select 1,'x','001','1001' Union All
Select 2,'x','002','1001' Union All
Select 3,'x','003','1001' Union All
Select 4,'w','xyz','1001'
GO
Select Max(f3) As f3 From tb Where f2='x'Select A.f3,A.f4 From tb A
Inner Join
(Select f2,Max(f3) As f3 From tb Where f2='x' Group By f2) B
On A.f2=B.f2 And A.f3=B.f3
GO
Drop Table tb
--Result
/*
f3
003f3 f4
003 1001
*/