2014-02-05 86 views
0

這是我的MSSQL的查詢任何人都可以幫助我將這個MSSQL查詢轉換爲ORACLE嗎?

UPDATE YT 
    SET  POSITION = RN 
        FROM ( 
        SELECT ROW_NUMBER() OVER (PARTITION BY PARENTID ORDER BY ID) - 1 RN 
        FROM CONVERTED 
        ) YT 

,這是我在嘗試將其轉換爲甲骨文

UPDATE CONVERTED 
    SET  POSITION = (SELECT ROW_NUMBER() OVER (PARTITION BY PARENTID ORDER BY ID) - 1 RN 
        FROM CONVERTED 
        ); 

Ufortunately我得到這個錯誤

SQL Error: ORA-01427: single-row subquery returns more than one row 
01427. 00000 - "single-row subquery returns more than one row" 

誰能幫助?

回答

1

假設id是獨一無二的,下面應在兩個數據庫工作:

UPDATE CONVERTED 
    SET POSITION = (select count(*) - 1 
        from converted c2 
        where c2.parentid = converted.parentid and 
          c2.id <= converted.id 
        ); 
+0

完美謝謝 – totalitarian

0

也許您正在尋找這樣的事情?

SQL> create table t (id int, parentid int, position int) 

2/

SQL> begin 
    2 insert into t values(1,null,null); 
    3 insert into t values(10,null,null); 
    4 insert into t values(3,1,null); 
    5 insert into t values(7,1,null); 
    6 insert into t values(4,1,null); 
    7 insert into t values(19,7,null); 
    8 insert into t values(11,7,null); 
    9 insert into t values(72,10,null); 
10 insert into t values(42,10,null); 
11 insert into t values(23,10,null); 
12 commit; 
13 end; 
14/

SQL> merge into t using 
    2 (select id, row_number() over (partition by parentid order by id) - 1 rn 
    3 from t) src 
    4 on (src.id = t.id) 
    5 when matched then 
    6 update set t.position = src.rn 
    7/

SQL> select * from t 
    2 start with parentid is null 
    3 connect by prior id = parentid 
    4 order siblings by position 
    5/

     ID PARENTID POSITION             
---------- ---------- ----------             
     1      0             
     3   1   0             
     4   1   1             
     7   1   2             
     11   7   0             
     19   7   1             
     10      1             
     23   10   0             
     42   10   1             
     72   10   2             
相關問題