Reconciliation Automation Query

944 views Asked by At

I have one database and time to time i change some part of query as per requirement. i want to keep record of results of both before and after result of these queries in one table and want to show queries which generate difference.

For Example, Consider following table

emp_id    country      salary
---------------------
1          usa         1000

2          uk          2500

3          uk         1200

4          usa          3500

5          usa          4000

6          uk          1100

Now, my before query is :

Before Query:

select count(emp_id) as count,country from table where salary>2000 group by country;

Before Result:

count     country

2      usa

1      uk

After Query:

select count(emp_id) as count,country from table where salary<2000 group by country;

After Query Result:

count     country

2      uk

1      usa

My Final Result or Table I want is:

column 1  |   column 2  |    column 3   |   column 4 |

2              usa            2             uk

1              uk              1             usa

...... but if query results are same than it shouldn't show in this table.

Thanks in advance.

1

There are 1 answers

0
Radim Bača On

I believe that you can use the same approach as here.

select t1.*, t2.* -- if you need specific columns without rn than you have to list them here
from
(
   select t.*, row_number() over (order by count) rn
   from
   (
     -- query #1
     select count(emp_id) as count,country from table where salary>2000 group by country;
   ) t
) t1
full join
(
   select t.*, row_number() over (order by count) rn
   from
   (
    -- query #2
    select count(emp_id) as count,country from table where salary<2000 group by country;
   ) t
) t2 on t1.rn = t2.rn