对 2 个查询进行比较以找出差异
do a compare on 2 queries to find differences
我有两个查询要相互比较。两者都查询 return 字段数和相同数据。我想要做的是能够找到一个查询中的哪些行在另一个查询中不存在,举个例子(这些查询非常简化,两个查询都有多个连接)
查询 1
Select Field1, Field2, Field3
from Tbl1
where date1 >='03/10/2020' and date1 <= '03/18/2020'
Q1 returns
Field1 Field2 Field3
--------------------------
123 BAK 03/10/2020
234 HAO 03/12/2020
查询 2
Select Field4, Field6, Field7
from Tbl2
where date1 >='03/10/2020' and date1 <= '03/18/2020'
查询 2 returns
Field4 Field6 Field7
--------------------------
123 BAK 03/10/2020
678 OID 03/17/2020
我需要在每个查询 Field1 = Field4、Field2 = Field6、Field3 = Field7 的三个字段上加入这些查询 return 以确保只有不完全匹配的数据是 returned.
我想要的结果基本上是 return query1 中在查询 2 中没有对应行的所有行,以及 query2 中在 query1 中没有对应行的所有行。
想要的结果:
234 HAO 03/12/2020 <--this exists in Query1 but not in Query2
678 OID 03/17/2020 <-- this exists in Query2 but not in Query1
所以我在这个过程中所做的是我创建了两个常见的 table 表达式,就像这样....然后我使用了 EXCEPT,但我想看看是否有更好的方法。
WITH QueryA AS
(
Select Field1, Field2, Field3
from Tbl1
where date1 >='03/10/2020' and date1 <= '03/18/2020'
),
QueryB AS
(
Select Field4, Field6, Field7
from Tbl2
where date1 >='03/10/2020' and date1 <= '03/18/2020'
)
使用UNION
:
Select Field1, Field2, Field3
from Tbl1
where date1 >= '2020-03-10' and date1 <= '2020-03-18'
UNION
Select Field4, Field6, Field7
from Tbl2
where date1 >= '2020-03-10' and date1 <= '2020-03-18';
FULL OUTER JOIN
也会有帮助:
SELECT COALESCE(t1.Field1, t2.Field4),
COALESCE(t1.Field2, t2.Field6),
COALESCE(t1.Field3 , t2.Field7)
FROM Tbl1 t1 FULL OUTER JOIN
Tbl2 t2
on t1.field1 = t2.field4 and t1.field2 = t2.Field6 and
t1.field3 = t2.field7
WHERE (t1.field1 is null or t2.field4 is null);
您可以使用 full join
-- 假设没有列是 NULL
:
select t1.*, t2.*
from (Select Field1, Field2, Field3
from Tbl1
where date1 >= '2020-03-10' and date1 <= '2020-03-18'
) t1 full join
(Select Field4, Field6, Field7
from Tbl2
where date1 >= '2020-03-10' and date1 <= '2020-03-18'
) t2
on t1.field1 = t2.field4 and t1.field2 = t2.field5 and
t1.field3 = t2.field7
where t1.field1 is null or t2.field4 is null;
另一种方法是union all
:
select t1.*, t2.*
from ((Select Field1, Field2, Field3, 1 as in_1, 0 as in_2
from Tbl1
where date1 >= '2020-03-10' and date1 <= '2020-03-18'
) t1 full join
(Select Field4, Field6, Field7, 0, 1
from Tbl2
where date1 >= '2020-03-10' and date1 <= '2020-03-18'
)
) t12
group by field1, field2, field3
having sum(in_1) = sum(in_2);
请注意,这比以前的方法有两个优点:
- 它处理列中的
NULL
个值。
- 它处理重复项 -- 并验证两个表中重复项的数量相同。
我正在玩一些虚拟数据,看看是否可以使用 2 left joins
来实现。看看有没有帮助
with cte1 (name, id) as
(select 'brad', 1 union all
select 'john',2),
cte2 (name, id) as
(select 'brad', 1 union all
select 'jack',3)
select a.name, a.id, 'missing in cte2' as status
from cte1 a left join cte2 b on a.name=b.name
where b.name is null
union
select a.name, a.id, 'missing in cte1' as status
from cte2 a left join cte1 b on a.name=b.name
where b.name is null
如果你真的想用except
,你可以
select name, id, 'missing in cte2' as status from cte1
except
select name, id, 'missing in cte2' as status from cte2
union
select name, id, 'missing in cte1' as status from cte2
except
select name, id, 'missing in cte1' as status from cte1
这是一种比较来自两个 tables/views/datasets
的数据的方法
select x.field1
,x.field2
,x.field3
,count(qry_1) as present_in_qry1
,count(qry_2) as present_in_qry2
from (Select Field1, Field2, Field3 , 1 as qry_1, null as qry_2
from Tbl1
where date1 >='03/10/2020' and date1 <= '03/18/2020'
union all
Select Field4, Field6, Field7, null as qry_1, 1 as qry_2
from Tbl2
where date1 >= '2020-03-10' and date1 <= '2020-03-18'
)x
group by x.field1
,x.field2
,x.field3
having count(qry_1) <> count(qry_2)
order by 1,2,3
我有两个查询要相互比较。两者都查询 return 字段数和相同数据。我想要做的是能够找到一个查询中的哪些行在另一个查询中不存在,举个例子(这些查询非常简化,两个查询都有多个连接)
查询 1
Select Field1, Field2, Field3
from Tbl1
where date1 >='03/10/2020' and date1 <= '03/18/2020'
Q1 returns
Field1 Field2 Field3
--------------------------
123 BAK 03/10/2020
234 HAO 03/12/2020
查询 2
Select Field4, Field6, Field7
from Tbl2
where date1 >='03/10/2020' and date1 <= '03/18/2020'
查询 2 returns
Field4 Field6 Field7
--------------------------
123 BAK 03/10/2020
678 OID 03/17/2020
我需要在每个查询 Field1 = Field4、Field2 = Field6、Field3 = Field7 的三个字段上加入这些查询 return 以确保只有不完全匹配的数据是 returned.
我想要的结果基本上是 return query1 中在查询 2 中没有对应行的所有行,以及 query2 中在 query1 中没有对应行的所有行。
想要的结果:
234 HAO 03/12/2020 <--this exists in Query1 but not in Query2
678 OID 03/17/2020 <-- this exists in Query2 but not in Query1
所以我在这个过程中所做的是我创建了两个常见的 table 表达式,就像这样....然后我使用了 EXCEPT,但我想看看是否有更好的方法。
WITH QueryA AS
(
Select Field1, Field2, Field3
from Tbl1
where date1 >='03/10/2020' and date1 <= '03/18/2020'
),
QueryB AS
(
Select Field4, Field6, Field7
from Tbl2
where date1 >='03/10/2020' and date1 <= '03/18/2020'
)
使用UNION
:
Select Field1, Field2, Field3
from Tbl1
where date1 >= '2020-03-10' and date1 <= '2020-03-18'
UNION
Select Field4, Field6, Field7
from Tbl2
where date1 >= '2020-03-10' and date1 <= '2020-03-18';
FULL OUTER JOIN
也会有帮助:
SELECT COALESCE(t1.Field1, t2.Field4),
COALESCE(t1.Field2, t2.Field6),
COALESCE(t1.Field3 , t2.Field7)
FROM Tbl1 t1 FULL OUTER JOIN
Tbl2 t2
on t1.field1 = t2.field4 and t1.field2 = t2.Field6 and
t1.field3 = t2.field7
WHERE (t1.field1 is null or t2.field4 is null);
您可以使用 full join
-- 假设没有列是 NULL
:
select t1.*, t2.*
from (Select Field1, Field2, Field3
from Tbl1
where date1 >= '2020-03-10' and date1 <= '2020-03-18'
) t1 full join
(Select Field4, Field6, Field7
from Tbl2
where date1 >= '2020-03-10' and date1 <= '2020-03-18'
) t2
on t1.field1 = t2.field4 and t1.field2 = t2.field5 and
t1.field3 = t2.field7
where t1.field1 is null or t2.field4 is null;
另一种方法是union all
:
select t1.*, t2.*
from ((Select Field1, Field2, Field3, 1 as in_1, 0 as in_2
from Tbl1
where date1 >= '2020-03-10' and date1 <= '2020-03-18'
) t1 full join
(Select Field4, Field6, Field7, 0, 1
from Tbl2
where date1 >= '2020-03-10' and date1 <= '2020-03-18'
)
) t12
group by field1, field2, field3
having sum(in_1) = sum(in_2);
请注意,这比以前的方法有两个优点:
- 它处理列中的
NULL
个值。 - 它处理重复项 -- 并验证两个表中重复项的数量相同。
我正在玩一些虚拟数据,看看是否可以使用 2 left joins
来实现。看看有没有帮助
with cte1 (name, id) as
(select 'brad', 1 union all
select 'john',2),
cte2 (name, id) as
(select 'brad', 1 union all
select 'jack',3)
select a.name, a.id, 'missing in cte2' as status
from cte1 a left join cte2 b on a.name=b.name
where b.name is null
union
select a.name, a.id, 'missing in cte1' as status
from cte2 a left join cte1 b on a.name=b.name
where b.name is null
如果你真的想用except
,你可以
select name, id, 'missing in cte2' as status from cte1
except
select name, id, 'missing in cte2' as status from cte2
union
select name, id, 'missing in cte1' as status from cte2
except
select name, id, 'missing in cte1' as status from cte1
这是一种比较来自两个 tables/views/datasets
的数据的方法select x.field1
,x.field2
,x.field3
,count(qry_1) as present_in_qry1
,count(qry_2) as present_in_qry2
from (Select Field1, Field2, Field3 , 1 as qry_1, null as qry_2
from Tbl1
where date1 >='03/10/2020' and date1 <= '03/18/2020'
union all
Select Field4, Field6, Field7, null as qry_1, 1 as qry_2
from Tbl2
where date1 >= '2020-03-10' and date1 <= '2020-03-18'
)x
group by x.field1
,x.field2
,x.field3
having count(qry_1) <> count(qry_2)
order by 1,2,3