shell/sql to get difference between two database


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers shell/sql to get difference between two database
# 1  
Old 09-23-2011
shell/sql to get difference between two database

I have two queries

query1

select IDW_SITE_NAME REGION,a.location_sid,a.CALENDAR_SID,count(*) from idw.vod_stream a, IDW_REF_DATA b
where a.location_sid = b.location_sid and a.calendar_sid between 20110901 and 20110915 --this is start date parameter and end date
parameter
and a.location_sid = 190 --this is location id parameter
and HAS_ERROR = 'N'
group by IDW_SITE_NAME,a.location_sid,a.CALENDAR_SID
order by a.location_sid,a.CALENDAR_SID

output

REGION LOCATION_SID CALENDAR_SID IDW COUNT
AUSTIN 190 20110901 110557
AUSTIN 190 20110902 132851
AUSTIN 190 20110903 168656

query2

select REGION,a.location_sid,a.CALENDAR_SID,count(*) from sda_vod_stream a, SDA_DIVISIONS b
where a.location_sid = b.location_sid and a.calendar_sid between 20110901 and 20110915
and a.location_sid = 190
group by REGION,a.location_sid,a.CALENDAR_SID
order by a.location_sid,a.CALENDAR_SID

output

REGION LOCATION_SID CALENDAR_SID SDA COUNT
AUSTIN 190 20110901 110559
AUSTIN 190 20110902 132851
AUSTIN 190 20110903 168659

both queries will be getting executed on separate database
e.g
query1-->executes on database1
query2-->executes on database2


now my requirement is that

here is final output

REGION LOCATION_SID CALENDAR_SID IDW COUNT SDA COUNT DIFF
AUSTIN 190 20110901 110557 110559 -2
AUSTIN 190 20110902 132851 132851 0
AUSTIN 190 20110903 168656 168659 -3

here difference = query1.count - query2.count

how can I produce a final output CSV /XLS file

I can generate above report file using oracle,but here both databases are different

I'm thinkning to copy above SQL in a shell script and can generate a report as oracle is based on UNIX

parameters

in above both query the changing parameter is
1. 20110901 this is start date parameter
2. 20110915 end date parameter
3. 190 this is location id

Please let me know if anybody has different solution for this ?
# 2  
Old 09-23-2011
Please edit your post to include code tags. I've just spent ten minutes trying to clean up your other thread!

Here is how:



Thanks.
# 3  
Old 09-23-2011
I have two queries

query1
Code:
 
select IDW_SITE_NAME REGION,a.location_sid,a.CALENDAR_SID,count(*) from idw.vod_stream a, IDW_REF_DATA b
where a.location_sid = b.location_sid and a.calendar_sid between 20110901 and 20110915 --this is start date parameter and end date 
parameter
and a.location_sid = 190 --this is location id parameter
and HAS_ERROR = 'N'
group by IDW_SITE_NAME,a.location_sid,a.CALENDAR_SID
order by a.location_sid,a.CALENDAR_SID

output

REGION LOCATION_SID CALENDAR_SID IDW COUNT
AUSTIN 190 20110901 110557
AUSTIN 190 20110902 132851
AUSTIN 190 20110903 168656

query2
Code:
 
select REGION,a.location_sid,a.CALENDAR_SID,count(*) from sda_vod_stream a, SDA_DIVISIONS b
where a.location_sid = b.location_sid and a.calendar_sid between 20110901 and 20110915
and a.location_sid = 190
group by REGION,a.location_sid,a.CALENDAR_SID
order by a.location_sid,a.CALENDAR_SID

output

REGION LOCATION_SID CALENDAR_SID SDA COUNT
AUSTIN 190 20110901 110559
AUSTIN 190 20110902 132851
AUSTIN 190 20110903 168659

both queries will be getting executed on separate database
e.g
query1-->executes on database1
query2-->executes on database2


now my requirement is that

here is final output

REGION LOCATION_SID CALENDAR_SID IDW COUNT SDA COUNT DIFF
AUSTIN 190 20110901 110557 110559 -2
AUSTIN 190 20110902 132851 132851 0
AUSTIN 190 20110903 168656 168659 -3

here difference = query1.count - query2.count

how can I produce a final output CSV /XLS file

I can generate above report file using oracle,but here both databases are different

I'm thinkning to copy above SQL in a shell script and can generate a report as oracle is based on UNIX

parameters

in above both query the changing parameter is
1. 20110901 this is start date parameter
2. 20110915 end date parameter
3. 190 this is location id

Please let me know if anybody has different solution for this ?

Last edited by qutesanju; 09-23-2011 at 06:27 PM..
# 4  
Old 09-24-2011
I would recommend creating a db link between those databases and do the required operation via sql code.

It should be much cleaner and easier then using shell.
Second choice would be using perl with DBI module.

Regards
Peasant.
# 5  
Old 09-24-2011
I agree with Peasant. This is why federation exists. Work smarter not harder Smilie
# 6  
Old 09-24-2011
I agree with Peasant Too
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Connect to database server and execute sql

I have a requirement and below is the detail. Create a shell script and needs to run in server "a". Connect to teradata database server "b". execute the .sql file from server "a" Save the output of the query to a file in server "a" Schedule this shell script to run every day for every 4... (1 Reply)
Discussion started by: MadhuSeven
1 Replies

2. Shell Programming and Scripting

SQL compare database

Hi , I have 2 databases which has same table names and I need to list entries of a particular table in database1 not existing in the same tablename of database2. eg : I have db1 having table name orders ad_d | code | start | end | amountscore | id | amount_class |... (1 Reply)
Discussion started by: siya@
1 Replies

3. Programming

pls. help with SQL database...

On the Linux systems, we have scripts which monitor disk usage. I want to take that data and feed that into SQL database. Can you give some advise. For example here is one of the file. I have many files like this but the format is same... 2012-09-24 04:36:14 2012-09-24 04:36:16 48414984... (3 Replies)
Discussion started by: samnyc
3 Replies

4. Shell Programming and Scripting

SQL in LINUX. Variable or Database

Dear Friends :-), Need your help once again. I have following SQL in a shell script to pick values from database select date_stamp, time_stamp from logs where date=31012012 However, at times we need to take these value(s) from a variable. To do this we have following query. select... (2 Replies)
Discussion started by: anushree.a
2 Replies

5. Shell Programming and Scripting

How to run a SQL select query in Oracle database through shell script?

I need to run a SQL select query in Oracle database and have to capture the list of retrieved records in shell script. Also i would like to modify the query for certain condition and need to fetch it again. How can i do this? Is there a way to have a persistent connection to oracle database... (9 Replies)
Discussion started by: vel4ever
9 Replies

6. UNIX for Advanced & Expert Users

Call parallel sql scripts from shell and return status when both sql are done

Hi Experts: I have a shell script that's kicked off by cron. Inside this shell script, I need to kick off two or more oracle sql scripts to process different groups of tables. And when both sql scripts are done, I will continue in the shell script to do other things like checking processing... (3 Replies)
Discussion started by: huasheng8
3 Replies

7. Programming

SQL for flat file database

I have a B+ tree flat file database which is used by the application we use. I would like to implement a SQL kind of access for that DB. I have implemented my data structures to parse and store the user written query for execution. But i would like to know, do we have any standard data... (2 Replies)
Discussion started by: kumaran_5555
2 Replies

8. Shell Programming and Scripting

Connecting to MS SQL database from bash (using unixODBC)

I've installed unixODBC and I would like to connect to a MS SQL (2005) database from a bash script. Can you post a code example? Thank you! :D J. (0 Replies)
Discussion started by: ph0enix
0 Replies

9. Shell Programming and Scripting

Calling SQL LDR and SQL plus scripts in a shell script

Hi- I am trying to achieve the following in a script so I can schedule it on a cron job. I am fairly new to the unix environment... I have written a shell script that reads a flat file and loads the data into an Oracle table (Table1) via SQLLDR. This Works fine. Then, I run a nested insert... (5 Replies)
Discussion started by: rajagavini
5 Replies

10. UNIX for Dummies Questions & Answers

Read SQL database from Unix...

Hi there, I am new here and looking for a bit of help... I don't don't know much about Unix but I've tried to include as much info as possible. We currently operate our system as a bespoke on SCO 5.05 with Informix databases. This includes a financials package called "Multisoft". Both... (0 Replies)
Discussion started by: uptheposh
0 Replies
Login or Register to Ask a Question