mysql 先求和后再算和的最大值
答案:3 悬赏:60 手机版
解决时间 2021-12-03 08:32
- 提问者网友:贪了杯
- 2021-12-02 14:30
mysql 先求和后再算和的最大值
最佳答案
- 五星知识达人网友:山河有幸埋战骨
- 2021-12-02 14:49
问题描述的不清楚。b,c是不是同一字段。你这里少个order 吧。order by a、
你要实现什么业务逻辑追问将b求和,然后求出这些分组里最大的sum(b)追答select sum(b) from t group by a order by sum(b) desc
你要实现什么业务逻辑追问将b求和,然后求出这些分组里最大的sum(b)追答select sum(b) from t group by a order by sum(b) desc
全部回答
- 1楼网友:山河有幸埋战骨
- 2021-12-02 17:18
SELECt a,MAX(b)V,sum(c) FROM t group by a
你是这个意思吧追问不是,将b求和,然后求出这些分组里最大的sum(b)追答db2 可以这样
select
t1.a
,max(v1) over() v1
,v2
from (SELECt a,sum(b)V1,sum(c) v2 FROM tablename1 group by a) t1
笨办法
select
t1.a
,t3v1
,t1.v2
from (SELECt a,sum(b)V1,sum(c) v2 FROM tablename1 group by a) t1
(select max(v1) v1 from (SELECt a,sum(b)V1,sum(c) v2 FROM tablename1 group by a) t2) t3
where 1=1
你是这个意思吧追问不是,将b求和,然后求出这些分组里最大的sum(b)追答db2 可以这样
select
t1.a
,max(v1) over() v1
,v2
from (SELECt a,sum(b)V1,sum(c) v2 FROM tablename1 group by a) t1
笨办法
select
t1.a
,t3v1
,t1.v2
from (SELECt a,sum(b)V1,sum(c) v2 FROM tablename1 group by a) t1
(select max(v1) v1 from (SELECt a,sum(b)V1,sum(c) v2 FROM tablename1 group by a) t2) t3
where 1=1
- 2楼网友:枭雄戏美人
- 2021-12-02 15:46
你这个里面包含了两成意思,
1.根据a字段 group
select a,sum(b) ,sum(c) from t
group by a
2.获得的结果再求最大值与其对应的A字段值
对不?如果上述分析正确,可以这么做
先求出最大和后赋给对应变量 max_s_b
select max( s_b) from
(select a,sum(b) s_b ,sum(c) s_c from t
group by a)tg1
再求出对应的最大值的记录
select a,sum(b) s_b ,sum(c) s_c from t
group by a
having sum(b) = max_s_b
1.根据a字段 group
select a,sum(b) ,sum(c) from t
group by a
2.获得的结果再求最大值与其对应的A字段值
对不?如果上述分析正确,可以这么做
先求出最大和后赋给对应变量 max_s_b
select max( s_b) from
(select a,sum(b) s_b ,sum(c) s_c from t
group by a)tg1
再求出对应的最大值的记录
select a,sum(b) s_b ,sum(c) s_c from t
group by a
having sum(b) = max_s_b
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯