协调自动化查询

时间:2017-12-04 09:26:22

标签: sql database-reconciliation

我有一个数据库,不时根据要求更改查询的某些部分。 我想在一个表中记录这些查询结果之前和之后的结果,并希望显示产生差异的查询。

例如, 请考虑以下表格

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

2          uk          2500

3          uk         1200

4          usa          3500

5          usa          4000

6          uk          1100

现在,我之前的查询是:

查询之前:

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

结果之前:

count     country

2      usa

1      uk

查询后:

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

查询结果后:

count     country

2      uk

1      usa

我想要的最终结果或表格是:

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

2              usa            2             uk

1              uk              1             usa

......但是如果查询结果相同,则不应该在此表中显示。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我相信您可以使用与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