Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to calculate time difference between two columns?

fahmedarainDec 10 2010 — edited Dec 10 2010
Hi,


How to calculate time difference between two dates columns?


thx

Edited by: user12007410 on Dec 10, 2010 2:03 AM

Comments

jaramill

You do understand that ROWNUM is a pseudo-column created by Oracle that just assigns a number to a row retrieved by the query and Oracle makes NO guarantees that the same rownum will be assigned to the same record every time you execute the query?

Also what you're really asking is the SET equivalent of INTERSECT.

Table A has records (1, 2, 3).

Table B has records (3, 4, 5).

Table A INTERSECT Table B = records (3).

Is this what you're asking for?

Please as always, define your actual table definition (column with data types) and any insert statements you may have, and also format your code using the "Advanced Editor" option and Use Syntax Highlighting -> SQL.

kendenny

How about

select count(*) into cnt

from (select 1 from t1 union all select 1 from t2);

GregV

Hi,

Yes this is one way to check if a table is empty. Depending on your logic, you can even use NULL instead of COUNT(*) and catch the NO_DATA_FOUND exception.

Frank Kulash

Hi,

944524 wrote:

Hi,

I am having two tables below where i need to check if any record present in either of two tables proceed something like this

select count(*) into cnt from T1 where rownum=1

select count(*) into cnt_1 from T2 where rownum=1


if cnt or cnt_1 > 0 then

proceed

OR expects a complete condition before the keyword "OR", as well as after it.

Correct syntax for 2 comparisons is like this:

IF  cnt > 0  OR cnt_1 > 0  THEN

To make it a little more efficient, you might want to eliminate the second query if the first one gets results, e.g.:

SELECT  COUNT (*)

INTO    cnt

FROM    t1

WHERE   ROWNUM = 1;

IF  cnt = 0  THEN

    SELECT  COUNT (*)

    INTO    cnt

    FROM    t2

    WHERE   ROWNUM = 1;

END IF;

IF  cnt > 0  THEN

...

944524

You mean to say use like below

select count(*) into cnt

from (select 1 from t1 where rownum = 1 union all select 1 from t2 where rownum = 1);

unknown-951199

944524 wrote:

You mean to say use like below

select count(*) into cnt

from (select 1 from t1 where rownum = 1 union all select 1 from t2 where rownum = 1);

SELECT COUNT(T1.*)+COUNT(T2.*) CNT FROM T1, T2;

IF CNT > 0 THEN

944524

This will impact the performance since both tables have huge data

Frank Kulash

Hi,

944524 wrote:

This will impact the performance since both tables have huge data

Really?  Isn't the whole point of this question that you don't know if either table has any data?

unknown-951199

944524 wrote:

You mean to say use like below

select count(*) into cnt

from (select 1 from t1 where rownum = 1 union all select 1 from t2 where rownum = 1);

post EXPLAIN PLAN for SQL above.

Do you realize & understand that SELECT COUNT(*) only ever returns one row; but the COUNT() is still forced to spin thru table (or PK index).

unknown-7404
I am having two tables below where i need to check if any record present in either of two tables proceed something like this

select count(*) into cnt from T1 where rownum=1

select count(*) into cnt_1 from T2 where rownum=1

if cnt or cnt_1 > 0 then

proceed

Ok - what is your question or issue?

What is it you need to 'proceed' with?

You need to actually ASK A QUESTION and give us ALL of the requirements and context.

1 - 10
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jan 7 2011
Added on Dec 10 2010
7 comments
115,540 views