aa bb cc
1 15 22
2 55 33
5 13 35
想实现bb字段最大值所在行的各字段信息。
我做的是
select aa,bb,cc
from a
where bb=max(bb);不知道错在哪里了
期望结果是:
aa bb cc
2 55 33
5 个解决方案
#1
where bb in (select max (bb) from a)
另,括号要用英文的括号,不要用中文的括号。
另,括号要用英文的括号,不要用中文的括号。
#2
USE tempdb
GO
IF OBJECT_ID('t') IS NOT NULL DROP TABLE t
GO
CREATE TABLE t(aa int,bb int,cc int)
INSERT INTO t
SELECT 1,15,22
UNION ALL SELECT 2,55,33
UNION ALL SELECT 5,13,35
--两种都可以
--1.
SELECT * FROM t AS a WHERE NOT EXISTS(
SELECT 1 FROM t AS b WHERE b.bb>a.bb
)
--2.
SELECT * FROM t WHERE bb=(SELECT MAX(bb) FROM t)
/*
aa bb cc
2 55 33
*/
#3
--测试数据
if not object_id(N'Tempdb..#a') is null
drop table #a
Go
Create table #a([aa] int,[bb] int,[cc] int)
Insert #a
select 1,15,22 union all
select 2,55,33 union all
select 5,13,35
Go
--测试数据结束
SELECT aa,bb,cc
FROM ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY bb DESC ) rn
FROM #a
) t
WHERE t.rn = 1
#4
万能的csdn,各位8点上班几分钟就帮我解决了,谢谢。
#5
没事就结贴吧
#1
where bb in (select max (bb) from a)
另,括号要用英文的括号,不要用中文的括号。
另,括号要用英文的括号,不要用中文的括号。
#2
USE tempdb
GO
IF OBJECT_ID('t') IS NOT NULL DROP TABLE t
GO
CREATE TABLE t(aa int,bb int,cc int)
INSERT INTO t
SELECT 1,15,22
UNION ALL SELECT 2,55,33
UNION ALL SELECT 5,13,35
--两种都可以
--1.
SELECT * FROM t AS a WHERE NOT EXISTS(
SELECT 1 FROM t AS b WHERE b.bb>a.bb
)
--2.
SELECT * FROM t WHERE bb=(SELECT MAX(bb) FROM t)
/*
aa bb cc
2 55 33
*/
#3
--测试数据
if not object_id(N'Tempdb..#a') is null
drop table #a
Go
Create table #a([aa] int,[bb] int,[cc] int)
Insert #a
select 1,15,22 union all
select 2,55,33 union all
select 5,13,35
Go
--测试数据结束
SELECT aa,bb,cc
FROM ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY bb DESC ) rn
FROM #a
) t
WHERE t.rn = 1
#4
万能的csdn,各位8点上班几分钟就帮我解决了,谢谢。
#5
没事就结贴吧