假设 tabaleA ID F2 F3 1 a 10 2 b 10 3 b 20 4 a 20 那么group by f2 后 f2 sumF3 a 30 b 30a的id有1,4 b的id有2,3 你要按id排,那么是a排前面还是b排前面? 也即, 是a取1,还是4,对应b取2还是3的问题.只有用聚合.别的不多说了.如果不理解,就去读下二维关系的定义吧.
select [f2], f3=sum(f3) from tableA t group by [f2] order by(select max(ID) from tableA where f2=t.t2) desc--以最小的那个ID为条件
select [f2],f3=sum(f3) from tableA group by [f2] having ordey by id
我的错了,要取一组都是最大的或者都是最小的idselect [f2],f3=sum(f3),(select max(ID) from tableA where f2=t.t2) as id from tableA group by [f2] having ordey by id
如果ID是
b
a
c
z
字符呢?
tabaleA
ID F2 F3
1 a 10
2 b 10
3 b 20
4 a 20 那么group by f2 后
f2 sumF3
a 30
b 30a的id有1,4
b的id有2,3
你要按id排,那么是a排前面还是b排前面? 也即, 是a取1,还是4,对应b取2还是3的问题.只有用聚合.别的不多说了.如果不理解,就去读下二维关系的定义吧.
[f2],
f3=sum(f3)
from
tableA t
group by [f2]
order by(select max(ID) from tableA where f2=t.t2) desc--以最小的那个ID为条件
select [f2],f3=sum(f3) from tableA group by [f2] having ordey by id