Home » SQL & PL/SQL » SQL & PL/SQL » Comparing two TimeStamp columns giving unexpected result (Oracle 11G)
Comparing two TimeStamp columns giving unexpected result [message #630287] Wed, 24 December 2014 03:06 Go to next message
Asfakul
Messages: 43
Registered: July 2014
Member
Hi All ,

My objective is this :

1. For a particular OBJECT_NM/OBJECT_KEY Combination I want to take the record which has the most recent value for LAST_UPD_TS field.

So from the given data, it will show only two rows. I have written the below query

select * from belk_db_stg_event gg,
(
select  object_key,
       object_nm,
       object_crud,
       last_upd_ts,
      max(last_upd_ts) keep(dense_rank last order by last_upd_ts asc) over (partition by object_nm,object_key) as Last_UPD
       from belk_db_stg_event) ff
  where ff.object_key=gg. object_key
  and ff.object_nm=gg.object_nm
  and ff.last_upd=gg.last_upd_ts;



But it's giving me all rows instead of giving just two rows. what I am doing wrong here Sad

I have attached the required table creation script and sample data.
Re: Comparing two TimeStamp columns giving unexpected result [message #630288 is a reply to message #630287] Wed, 24 December 2014 03:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Employees that have been the last hired one in each department:
SQL> with 
  2    data as (
  3      select ename, deptno, hiredate,
  4             rank() over (partition by deptno order by hiredate desc) rk
  5      from emp
  6    )
  7  select deptno, ename, hiredate
  8  from data
  9  where rk = 1
 10  order by deptno
 11  /
    DEPTNO ENAME      HIREDATE
---------- ---------- -------------------
        10 MILLER     23/01/1982 00:00:00
        20 ADAMS      23/05/1987 00:00:00
        30 JAMES      03/12/1981 00:00:00

[Updated on: Wed, 24 December 2014 03:28]

Report message to a moderator

Re: Comparing two TimeStamp columns giving unexpected result [message #630352 is a reply to message #630288] Thu, 25 December 2014 02:12 Go to previous messageGo to next message
Asfakul
Messages: 43
Registered: July 2014
Member
Thanks a lot for replying. That solves my problem.

But I am really want to know why the timestamp comparison did not work in my case? What is the proper way to compare two timestamp columns.
Re: Comparing two TimeStamp columns giving unexpected result [message #630354 is a reply to message #630352] Thu, 25 December 2014 04:32 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

A join condition is missing: "ff.last_upd=gg.last_upd_ts".


Re: Comparing two TimeStamp columns giving unexpected result [message #630379 is a reply to message #630354] Thu, 25 December 2014 09:10 Go to previous messageGo to next message
Asfakul
Messages: 43
Registered: July 2014
Member
It' there. I have had added the join condition
Re: Comparing two TimeStamp columns giving unexpected result [message #630383 is a reply to message #630379] Thu, 25 December 2014 11:15 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Sorry I meant (wrong line copied and pasted): "ff.last_upd=gg.last_upd".

Previous Topic: PLS-00103: Encountered the symbol "END" when expecting one of
Next Topic: Divide in Groups
Goto Forum:
  


Current Time: Wed Aug 26 05:21:35 CDT 2026