Solved

How to write a SQL query from 2 separate databases

Posted on 2011-02-25
7
597 Views
Last Modified: 2012-08-13
I did not search because I don't know exactly how to phrase this.  I will present the scenario and ask that someone provide me with an example of what I need or a link to an example.

I have 2 separate oracle databases.  I am looking to get data from one table in each database and then provide computed results to a report.
The first table in db 1 is a statistics table, the table in db2 is a corrections table.  They will link on username.

The pseudo code for what I want is:  Select username, sum(errors) from db2  also select username sum(pagesprocessed) from db1  and both where date_stamp between startdate and enddate provided by a webform.  The results will be displayed by username as column 1 and column 2 being the sum(pagesprocessed) / sum(errors).  

I have done this in a prior application but I did it by pulling 2 recordsets populating 2 arrays and looping the arrays.  I would like to know : 1 is it possible to do in a single query and 2 can someone provide an example or a link to an example.  And know that one of the DBs does link to the other DB through a DBLINK.   Thanks.
0
Comment
Question by:thamilto0410
  • 3
  • 2
  • 2
7 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 34979485
assuming link is from db1 to db2...

Select t2.username, sum(t2.errors)/sum(t1.pagesprocessed)  from table1 t1, table2@db2_link t2
where t1.date_stamp between startdate and enddate
and t2.date_stamp between startdate and edndate
and t1.username = t2.username
group by t2.username
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 400 total points
ID: 34979491
if link is from db2 to db1


Select t2.username, sum(t2.errors)/sum(t1.pagesprocessed)  from table1@db1_link t1, table2 t2
where t1.date_stamp between startdate and enddate
and t2.date_stamp between startdate and edndate
and t1.username = t2.username
group by t2.username
0
 
LVL 1

Author Comment

by:thamilto0410
ID: 34984789
sdstuber:  Sorry for the delay after I sent the question my computer at work went down I was not able to get back to you till now.  Looks exactly like what I needed to see an example of.  One last question suppose some of the users have pages but not errors.  That would be division by zero which is not possible how do I handle that?  Thanks.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 34984801
What do want to be returned if this happens?
0
 
LVL 1

Author Comment

by:thamilto0410
ID: 34985082
slightwv: Thank you for offering to assist.  The report will show users, errorcount, page count and then a %  dividing the errors into the pages.  There will always be pages.  If there are no errors I would like to show 0 for the errors and something like n/a for the division.  Thanks.
0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 100 total points
ID: 34985215
Based on the logic provided by sdstuber see if this helps.  If there's something missing please add to the test to show the problem.
drop table tab1 purge;
create table tab1(errors number, pagesprocessed number);

insert into tab1 values(1,0);
insert into tab1 values(1,0);

-- pagesprocessed = 0, trap error
select nvl(to_char(sum(errors)/case sum(pagesprocessed) when 0 then null else sum(pagesprocessed) end),'n/a')
from tab1;

insert into tab1 values(1,1);

-- pagesprocessed = 1, show results
select nvl(to_char(sum(errors)/case sum(pagesprocessed) when 0 then null else sum(pagesprocessed) end),'n/a')
from tab1;

Open in new window

0
 
LVL 1

Author Closing Comment

by:thamilto0410
ID: 34987727
Thank you both.  I had not written anything like this before and the answers provided got me to a working solution with the sql offered by sdstuber and the nvl function offered by slightwv..
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

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…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

785 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