我有两张桌子:
表A
ID TYPE SDATE EDATE RATING 1 M 1/1/2010 1/1/2011 A 1 M 1/3/2010 1/4/2011 B 2 A 7/2/2010 1/31/2015 C 3 M 3/1/2011 1/20/2012 B 4 A 3/1/2011 1/20/2012 B 5 M 3/1/2009 3/1/2009 F 6 M 12/31/2006 12/31/9999 A 7 A 1/1/2006 12/31/9999 B
表B
ID TYPE SDATE EDATE RATING 1 M 1/1/2010 1/1/2011 A 2 A 7/2/2010 1/31/2015 C 2 A 9/2/2010 1/31/2015 C 3 M 3/1/2011 1/20/2012 B 4 A 3/1/2011 1/20/2012 C 6 M 12/31/2006 12/31/2015 A 7 A 2/1/2006 12/31/9999 B 8 M 1/2/2010 1/2/2012
当我执行表A减去表B时,它会得到下一个结果:
ID TYPE SDATE EDATE RATING 1 M 1/3/2010 1/4/2011 B 4 A 3/1/2011 1/20/2012 B 5 M 3/1/2009 3/1/2009 F 6 M 12/31/2006 12/31/9999 A 7 A 1/1/2006 12/31/9999 B
我已经确定了A.id不在B.id中的情况,但我如何检查其他列中的不同之处?
我不允许在架构上创建任何内容,因此我必须通过查询来完成。
到目前为止,这就是我所拥有的,但它给了我不正确的数据。
with aminusb as
(
select
*
from A
minus
select
*
from B
)
select
diff.*
from
aminusb diff
,B
where
diff.id = B.id
and
diff.start_date = B.start_date
and
diff.end_date = B.end_date
and
diff.rating <> B.rating
你知道我怎么能得到不同的场景?
我需要获取满足每个场景的记录列表,以便人们可以详细检查它。
感谢。
答案 0 :(得分:1)
这会产生不常见的行:
Select startdate, enddate, rating
from TableA
Intersect
Select startdate, enddate, rating
from TableB
显示所有记录:
Select TableA.startdate, TableA.enddate, TableA.rating
from TableA
UNION
Select startdate, enddate, rating
from TableB
案例1:
Select TableA.*, TableB.*
from tableA,TableB
Where TableA.StartDate = tableB.StartDate
And tableA.Enddate=TableB.Enddate
And TableA.Rating <>TableB.Rating
TableA.Rating = TableB.Rating
案例2:
Select TableA.*, TableB.*
from tableA,TableB
Where TableA.StartDate = tableB.StartDate
And tableA.Enddate<>TableB.Enddate
And TableA.Rating = TableB.Rating
案例3:
Select TableA.*, TableB.*
from tableA,TableB
Where TableA.StartDate <> tableB.StartDate
And tableA.Enddate<>TableB.Enddate
And TableA.Rating <>TableB.Rating
And TableA.ID =TableB.ID