0

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

例如,考虑下表

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

......但如果查询结果与此表中不应显示的结果相同。

提前致谢。

4

1 回答 1

0

我相信您可以使用与此处相同的方法。

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
于 2017-12-05T07:38:51.280 回答