Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 608
  • Last Modified:

How to write a SQL query from 2 separate databases

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
thamilto0410
Asked:
thamilto0410
  • 3
  • 2
  • 2
2 Solutions
 
sdstuberCommented:
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
 
sdstuberCommented:
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
 
thamilto0410Author Commented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
slightwv (䄆 Netminder) Commented:
What do want to be returned if this happens?
0
 
thamilto0410Author Commented:
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
 
slightwv (䄆 Netminder) Commented:
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
 
thamilto0410Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now