OVER分区未提供正确的结果

时间:2019-02-10 06:12:27

标签: sql .net sql-server

public void send(Map<String, List<Integer>> mapOf){

     mapOf.forEach((k ,v) -> {
         v.stream().forEach(o ->{
             myList.add(o)
             message.push(o);
         });
     });
    }

给出结果

message.push(o), pushes data to the database. <br/>

但是我需要

之类的结果
select ite_code,specification,x_unit , 
Cast (( Row_number() OVER( partition BY ite_code, specification, x_unit   ORDER BY ite_code, specification, x_unit ) ) AS  NUMERIC(18, 4))  partition_no  from (
select ite_code ='110001010001', specification= 'Sample 1',x_unit= 'NOS' union all 
select ite_code ='110001010001', specification= 'Sample 1',x_unit= 'NOS' union all 
select ite_code ='110001010001', specification= 'Sample 2',x_unit= 'NOS' union all 
select ite_code ='110001010001', specification= 'Sample 2',x_unit= 'NOS'    ) a

我的代码有什么问题?

1 个答案:

答案 0 :(得分:4)

也许您打算在此处使用等级函数:

SELECT
    ite_code,
    specification,
    x_unit, 
    DENSE_RANK() OVER (ORDER BY ite_code, specification, x_unit) AS partition_no
FROM yourTable
ORDER BY
    ite_code,
    specification,
    x_unit;

我对查询所做的主要更改是将ROW_NUMBER交换为DENSE_RANK,并删除了分区。