在oracle中添加一个列的序列

时间:2015-11-03 20:25:31

标签: sql oracle sql-update

我有一张桌子:TENDERINFO。有OrderNo,Tender,TenderSequence

我需要从1开始更新TenderSequence并继续执行OrderNo中的订单项数量。

我如何实现这一目标?

3 个答案:

答案 0 :(得分:0)

所以我们要按OrderNo进行“分组”行编号。我无法判断Tender或TenderSequence是否可以作为您数据的排序列,所以我只是使用OrderNo。这显示使用窗口函数row_number()获取rownumber:

  static class A<T extends Comparable<? super T>> {
    Stream<T> s = Stream.empty();
    public T min() { return s.min((t1, t2) -> t1.compareTo(t2)).orElse(null); }
    public T max() {
        T t = s.max(T::compareTo).orElse(null);
        return t; }
 }

System.out.println(a.max()); //null

答案 1 :(得分:0)

WITH cte as (
   SELECT OrderNo,
          Tender,
          ROW_NUMBER() OVER ( ORDER BY OrderNo ) as rn,
          ROW_NUMBER() OVER ( PARTITION BY OrderNo ORDER BY OrderNo ) as rn2
    FROM TENDERINFO
   ) 
UPDATE TENDERINFO T
SET TenderSequence = (SELECT c.rn
                      FROM cte c
                      WHERE c.OrderNo = T.OrderNo
                        AND c.Tender = T.Tender
                     )

不确定您需要哪个row_number。

在您的问题中,如果每个Tender可以有多个OrderNo

,则不清楚

在这种情况下,您可以使用

OVER ( ORDER BY OrderNo, Tender )

OVER ( ORDER BY OrderNo, TenderSequence )

答案 2 :(得分:0)

如果您只是尝试使用序列设置表,从最小OrderNo的1开始到number_of_rows到最大的OrderNo,以下查询可以实现:

merge into tenderinfo t 
  using (
    select 
        orderno, 
        rownum rn 
      from (select orderno from TENDERINFO order by orderno)
  ) seq on (seq.orderno = t.orderno )
  when matched then
  update set t.tendersequence = seq.rn ;