Solved

Problem updating selected rows (with rank)

Posted on 2007-11-24
3
335 Views
Last Modified: 2010-03-20
HI i need to convert this to an update and i have no success :(
Here is the select

select rn, rdt, id_cpt_inet, no_tel, dt_deb, dt_fin, dt_com, dt_eff_inet, cd_stat_approv, no_tp_compo
from (
  select
  rank() over (
    partition by no_tel, id_cpt_inet
    order by
        nvl (dt_com, to_date(19000101,'yyyymmdd')) desc,
        nvl (dt_fin, to_date(20090101,'yyyymmdd')) desc,
        nvl (dt_deb, to_date(20090101,'yyyymmdd')) desc
  )
     rn, rdt, id_cpt_inet, no_tel, dt_deb, dt_fin, dt_com, dt_eff_inet, cd_stat_approv, no_tp_compo
  from (
      select
      rank() over (
            partition by id_cpt_inet
            order by dt_eff_inet desc
      )
      rdt, id_cpt_inet, no_tel, dt_deb, dt_fin, dt_com, dt_eff_inet, cd_stat_approv, no_tp_compo
      from cleaned_sga_tpc
  ) where rdt =1
) where rn=1 and cd_stat_approv != 8 and cd_stat_approv != 1 and no_tp_compo = 11

I need to update the table cleaned_sga_tpc and for each of those record set it to the same value (1 for exemple), basically tagging my good records selected.

I tried with a select for update in a cursor and it seems to not like the analytics commands :(
0
Comment
Question by:beaudoin_n
  • 2
3 Comments
 
LVL 27

Expert Comment

by:sujith80
Comment Utility
Try this :
update cleaned_sga_tpc X
set <your col> = (select rn FROM
(
select rn, r_id
from (
  select
  rank() over (
    partition by no_tel, id_cpt_inet
    order by
        nvl (dt_com, to_date(19000101,'yyyymmdd')) desc,
        nvl (dt_fin, to_date(20090101,'yyyymmdd')) desc,
        nvl (dt_deb, to_date(20090101,'yyyymmdd')) desc
  )
     rn, r_id, rdt, id_cpt_inet, no_tel, dt_deb, dt_fin, dt_com, dt_eff_inet, cd_stat_approv, no_tp_compo
  from (
      select
      rowid rid,
      rank() over (
            partition by id_cpt_inet
            order by dt_eff_inet desc
      )
      rdt, id_cpt_inet, no_tel, dt_deb, dt_fin, dt_com, dt_eff_inet, cd_stat_approv, no_tp_compo
      from cleaned_sga_tpc
  ) where rdt =1
) where rn=1 and cd_stat_approv != 8 and cd_stat_approv != 1 and no_tp_compo = 11
)
where r_id = X.rowid )
/
0
 

Author Comment

by:beaudoin_n
Comment Utility
doesn't work.. tells me r_id is invalud identifier ...
0
 
LVL 27

Accepted Solution

by:
sujith80 earned 250 total points
Comment Utility
This is the logic behind the solution i've given. You can make the changes and see how it works.

SQL> select * from tbl1;

        ID DT
---------- ---------
           28-NOV-07
           29-NOV-07
           27-NOV-07
           26-NOV-07

SQL> update tbl1 t
  2  set id = ( select rn
  3             from (select rowid r_id , row_number() over (order by dt) rn
  4                   from tbl1 )
  5             where r_id = t.rowid)
  6  /

4 rows updated.

SQL> select * from tbl1;

        ID DT
---------- ---------
         3 28-NOV-07
         4 29-NOV-07
         2 27-NOV-07
         1 26-NOV-07
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now