Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problem updating selected rows (with rank)

Posted on 2007-11-24
3
338 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
ID: 20346141
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
ID: 20346837
doesn't work.. tells me r_id is invalud identifier ...
0
 
LVL 27

Accepted Solution

by:
sujith80 earned 250 total points
ID: 20363282
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SYbase 4 36
how to find out the count of records based on the subfolders paths 11 31
SQL - format decimal in a string 5 38
SQL Syntax Grouping Sum question 7 24
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that useā€¦
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

789 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