我想根据s_fund分类 拿到b最大值的那一行 应该怎么写sql。
就比如我想要获取s_fund=aaa和s_fund=bbb中的b最大的那一行和 就是id=3和id=7的那两行,我用group by s_fund 但是就拿不到s_date的值。 因为这个数据也是经过多次子查询查出来的,再嵌套一层子查询的话会很复杂
select s_date from tablename where b in (select max(b) from tablename group by s_found)
这样就可以了,用了group by 前面就要用函数而不是某个字段。
SELECT s_fund,max(b) as maxb FROM test1
GROUP BY s_fund
看下此图;符合你说的 获取s_fund=aaa和s_fund=bbb中的b最大的那一行;
select * from (
select *,row_number() over(partition by s_fund order by b desc ) rn from tablename )
where rn = 1