sql - Selecting top n Oracle records with ROWNUM still valid in subquery? -
i have following firebird query:
update hrs h set h.plan_week_id= (select first 1 c.plan_week_id calendar c c.calendar_id=h.calendar_id) coalesce(h.calendar_id,0) <> 0
(intention: records in hrs
(non-zero) calendar_id
take calendar.plan_week_id
, put in hrs.plan_week_id
)
the trick select first record in oracle use where rownum=1, and if understand correctly i not have use rownum in separate outer query because 'only' match rownum=1 - suggesting questions may have answer ;-)
this make it
update hrs h set h.plan_week_id= (select c.plan_week_id calendar c (c.calendar_id=h.calendar_id) , (rownum=1)) coalesce(h.calendar_id,0) <> 0
i'm using 'first record' selection of one field guarantee one value can put h.plan_week_id
.
question: will above query work under oracle intended?
right now, not have filled oracle db @ hand run query on.
like nicholas krasnov said, can test in sql fiddle.
but if ever find use rownum = 1 in subquery, alarm bells should go off, because in 90% of cases doing wrong. need random value. when selected values same, rownum = 1 valid.
in case expect calendar_id primary key in calendar. therefor each record in hrs can have 1 plan_week_id selected per record. rownum = 1 not required.
and answer question: yes, run fine. though brackets around each clause not required , in fact confusing (me).
Comments
Post a Comment