Reconciliation Automation Query

醉酒当歌 提交于 2019-12-08 10:06:45

问题


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:


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


来源:https://stackoverflow.com/questions/47630095/reconciliation-automation-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!