Skip to Main Content
  • Questions
  • Number of rows inserted in last 24 hours

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Vinay.

Asked: February 06, 2008 - 11:54 am UTC

Last updated: February 08, 2008 - 3:16 pm UTC

Version: 10.2.0.3

Viewed 10K+ times! This question is

You Asked

Hi Tom,

I have a table with two columns.

Create table tab(name varchar2(10),value number);


Following rows are inserted on Feb 3rd 2008

kc 2
vc 3
vk 4


Following rows are inserted on Feb 4th 2008

cv 1
ck 5

Following rows are inserted on Feb 5th 2008

vv 7
kk 8
kv 9
k1 10


Table now has
SQL>select * from tab;
NAME VALUE
-------------------- ----------
kc 2
vc 3
vk 4
cv 1
ck 5
vv 7
kk 8
k1 10


This table doesn't have a any date field. I would like to know if there is way to get the rows that are added ( or inserted ) each day and the date on which the rows are added.


I would like a query which woule give me the number of rows added each day

This is what I would like to see

numofrows date
--------- -----
3 03-FEB-08
2 04-FEB-08
3 05-FEB-08


Suggestions Appreciated,

Thank you

Vinay











and Tom said...

not really, this would be something YOU would have done - you would have put a "created_on" attribute knowing you needed this information. and then counting would be easy (assuming you never DELETE of course, if you DELETE - then forget it)

Right now, if you cannot add that column, you can add a job to your database that every night at just past midnight or close to midnight you:

insert into my_table
select trunc(sysdate-1) dt, count(*) cnt
from that_table;

so, now you have a count of the rows in the table every night. a simple


select dt, cnt - lag(cnt) over (order by dt) last_cnt rows_added
from my_table;

will do it.

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Vinay Chandupatla, February 08, 2008 - 9:55 am UTC

I was just wondering if there was a way Oracle Stores the timestamp for each row as psuedo column (like rowid ). I was trying to avoid creating new tables.

Anyway I am going use the technique you have suggested.

The following table will also have and amount field. I will also have to do a sum(amount) each day. I think I can use the same technique to achieve the results.

insert into my_table
select trunc(sysdate-1) dt, count(*) cnt,sum(amount) amt
from that_table;

The above query runs as a job everyday at 6am



select dt, cnt - lag(cnt) over (order by dt) last_cnt rows_added, amt-lag(amt) over(order by dt)
from my_table;


The above queriy gives the number of rows added and their total of the amounts.


Let me know if the above works,
Thank you,
VInay






Tom Kyte
February 08, 2008 - 3:16 pm UTC

There is this thing called the ora_rowscn (search for it on this site)

You'd have to rebuild the tables to enable it at the row level (works at the block level)

and it would only go back for about 5 days using the scn to timestamp function (the scn would go back forever, but you'd have to remember the scn's for each day - what the ending scn was)


yes, the lag query would work

Oracle CDC

illiyaz, February 09, 2008 - 10:32 am UTC

Vinay,I am not sure if you have heard of Oracle CDC.It is pretty kool feature and not that hard to set up.Solves all the incremental load headaches and is pretty quick too.

More to Explore

Analytics

Analytic SQL got you confused? Check out Connor McDonald's complete video course.