Q
need add increase rank num inside each type
Table
type | total |
---|---|
car | 10 |
car | 22 |
truck | 33 |
Sql
select (@i := case when @pre_type=type then @i+1 else 1 end) as rownum, t.*, (@pre_type:=type) as pre_type
from chang_moto_table t, (select @i:=0,@pre_type:='') as sidecar
group by t.total,t.type
order by t.type
Result
rownum | type | total | pre_type |
---|---|---|---|
1 | car | 10 | car |
2 | car | 22 | car |
1 | truck | 33 | truck |
Info
- only test easy case
- not fast
- search Rank Over in Mysql
- consider use temp view
本文由 Ivan Dong 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Jul 7, 2023 at 04:26 am