简体   繁体   中英

making an update query through a subquery using ROW_NUMBER()?

This query does not work ERROR: 01427. 00000 - "single-row subquery returns more than one row":

UPDATE table_name SET column_update =( 
  SELECT ROW_NUMBER() 
  OVER(order by column_day_job) from table_name
) WHERE column_pearson_id = 808;

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

This SELECT works perfectly. I want to copy the ROW_NUMBER () values for sequence_day_job column, in the same order

SELECT  column_person_id, column_day_job, sequence_day_job,
ROW_NUMBER()
  OVER (PARTITION BY column_person_id
      ORDER BY column_day_job)
  FROM table_name;

UPDATE 10/09/2015 (01) - UPDATE tried to accomplish something like this, but it did not work, if a temporary table would solve my problem? It's something that seems to be relatively simple for the use of a temporary table, and cmo will be a recurring believe that use of temporary table is not so interesting.

 UPDATE table_name SET sequence_day_job = (
 SELECT ROW_NUMBER() OVER (PARTITION BY column_person_id ORDER BY column_day_job)  
       FROM PC_COMENTARIO )xx

)WHERE column_person_id = 808; //ERROR: 01427. 00000 - "single-row subquery returns more than one row"

This should work:

MERGE INTO table_name t
USING ( SELECT rowid rid, row_number() OVER (order by sequence_job_day) rn FROM table_name ) u
ON ( t.rowid = u.rid AND t.column_pearson_id = 888 )
WHEN MATCHED THEN UPDATE SET t.column_update = u.rn;

Note -- column_update will be unique across all rows in the table. If you just want a unique ordering within each person, you need to PARTITION BY column_pearson_id . Also, then you could put the =888 condition in the USING clause and take it out of the MATCH clause.

Why do you need a subquery. Doesn't this work:

UPDATE table_name
SET column_update = column_day_job

UPDATE - I think you need something like this. The syntax might be off, but I think you need you need to create a virtual table that has the row numbers you need & then pick out the correct row. If the dates in that table are unique, then you can use those.

UPDATE table_name t1 SET column_update =( 
  SELECT t1.rownum
  from (select ROW_NUMBER() from table_name t2) t3
  where t3.seq_day_job = t1.seq_day_job
) WHERE column_pearson_id = 808;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM