我有一个包含以下字段的表:season,collection,product_key,aggregated_sale。我想添加额外的排名列(自动递增),它应该按季节排序,集合,aggregated_sale
假设我有
ss, f1, 2, 5
ss, f1, 3, 10
ss, f1, 1, 11
ss, f2, 4, 7
ss, f2, 5, 11
预期产出
ss, f1, 2, 5,1
ss, f1, 3, 10,2
ss, f1, 1, 11,3
ss, f2, 4, 7,1
ss, f2, 5, 11,2
答案 0 :(得分:0)
这个应该做的工作,虽然效率不高
select col1, col2,col3,col4,
(
select count(*) from myTable where
col1<t.col1 or
(col1=t.col1 and col2<t.col2) or
(col1=t.col1 and col2=t.col2 and col4<t.col4)
)+1 as col5
FROM myTable t
order by col1,col2
注意:
答案 1 :(得分:0)
以下查询解决了我的问题
set @type = '';
set @num = 1;
select
ap.*,
@num := if(@type = concat(collection,forecast_name), @num + 1, 1) as row_number,
@type := concat(collection,forecast_name) as dummy
from rank_processing.aggregate_product ap;