?
Solved

Problem updating selected rows (with rank)

Posted on 2007-11-24
3
Medium Priority
?
341 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
[X]
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
  • 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 1000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

752 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