求某字段值最大的那条记录的另一个字段的值

cym7808 2005-09-14 06:04:08
某表tb_a,列c_a,c_b
求c_b的值为最大的那条记录的c_a的值
请写出具体语句
...全文
250 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
bugchen888 2005-09-15
  • 打赏
  • 举报
回复
楼上正解
zyfire88 2005-09-14
  • 打赏
  • 举报
回复
还要把这个值赋给一个参数@H,
________________________________

declare @H 数据类型
set @H=(select top 1 c_a from tb_a group by c_a order by sum(c_b) desc)
clxxj 2005-09-14
  • 打赏
  • 举报
回复
select c_a from (select top 1 c_a, sum(c_b) c_b
from tb_a
group by c_a
order by sum(c_b) desc) a

这样还不行么?
ReViSion 2005-09-14
  • 打赏
  • 举报
回复
--试试
Select top 1 * (Select C_a,sum(C_b) C_b from tb_a
group by C_a)a
order by C_b desc
cym7808 2005-09-14
  • 打赏
  • 举报
回复
还要把这个值赋给一个参数@H,
zyfire88 2005-09-14
  • 打赏
  • 举报
回复
描述不清,重描一次

某表tb_a,列c_a,c_b
首先按c_a分组,将c_b求和
求按c_a分组,将c_b求和后
sum(c_b)的值为最大的那条记录的c_a的值
请写出具体语句
_____________________________________________________________

select top 1 c_a from tb_a group by c_a order by sum(c_b) desc
iwl 2005-09-14
  • 打赏
  • 举报
回复
select c_a from tb_a where c_b=(select max(c_b) from tb_a)
xueguang 2005-09-14
  • 打赏
  • 举报
回复
select c_a from (select top 1 c_a, sum(c_b) c_b
from tb_a
group by c_a
order by sum(c_b) desc) a
filebat 2005-09-14
  • 打赏
  • 举报
回复
--测试数据
create table [tb_a] ([c_a] [int] null,[c_b] [int] null)
go
insert into tb_a
select 1,3 union
select 1,2 union
select 2,4 union
select 2,7 union
select 3,9
--查询
select top 1 c_a, sum(c_b)
from tb_a
group by c_a
order by sum(c_b) desc
--清除
drop table tb_a
云中客 2005-09-14
  • 打赏
  • 举报
回复
create table [tb_a] (
[c_a] [int] null,
[c_b] [int] null
)
go
insert into tb_a
select 1,3 union
select 4,2 union
select 8,4 union
select 7,7 union
select 3,9
select * from tb_a
select c_a from tb_a where c_b=(select max(c_b) from tb_a)
select a.c_a from tb_a a where not exists(select 1 from tb_a where c_b>a.c_b)
云中客 2005-09-14
  • 打赏
  • 举报
回复
select c_a from tb_a where c_b=(select max(c_b) from tb_a)
select a.c_a from tb_a a where not exists(select 1 from tb_a where c_b>a.c_b)

两个语句都可以,相对而言,好像第一个更好理解
cym7808 2005-09-14
  • 打赏
  • 举报
回复
描述不清,重描一次

某表tb_a,列c_a,c_b
首先按c_a分组,将c_b求和
求按c_a分组,将c_b求和后
sum(c_b)的值为最大的那条记录的c_a的值
请写出具体语句


gimy007 2005-09-14
  • 打赏
  • 举报
回复
select c_a from tb_a where c_b in (select max(c_b) from tb_a)
vivianfdlpw 2005-09-14
  • 打赏
  • 举报
回复
select c_a
from tb_a
where c_b=(select max(c_b) from tb_a)
子陌红尘 2005-09-14
  • 打赏
  • 举报
回复
select a.c_a from tb_a a where not exists(select 1 from tb_a where c_b>a.c_b)
子陌红尘 2005-09-14
  • 打赏
  • 举报
回复
select c_a from tb_a where c_b=(select max(c_b) from tb_a)

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧