select
t.slotmachinebk,
t.gamingdate,
t.freeplaydownloaded,
t.freeplayadjusted,
t.freeplayplayed,
t.freeplayabandoned,
t.freeplaybalance
from (select * from freeplay.egmfreeplay union all select * from Change.EgmFreePlay) t where not exists (select * from testtable where
slotmachinebk = t.slotmachinebk and
auditdate = t.gamingdate and
freeplaydownloaded = t.freeplaydownloaded and
freeplayadjusted = t.freeplayadjusted and
freeplayplayed = t.freeplayplayed and
freeplayabandoned = t.freeplayabandoned and
freeplaybalance = t.freeplaybalance)
我有这个tsql查询,它给了我testtable和freeplay.egmfreeplay之间不匹配的记录....但是如何修改此查询以获取不匹配的列名/值,列名为列用于子查询AND ...
答案 0 :(得分:3)
你可以在SELECT查询中放入一堆'CASE WHEN'语句
CASE WHEN a.column1 <> b.column1 THEN 1 ELSE 0 END AS column1_diff
然后你会在你的结果集中看到一个'1',如果该列对于那个记录是不同的,或者如果它没有不同则为'0'
修改强>
我试图将您的查询重构为可行的内容。
SELECT t.*,
CASE WHEN t.slotmachinebk <> z.slotmachinebk THEN 1 ELSE 0 END AS slotmachinebk_diff,
CASE WHEN t.gamingdate <> z.gamingdate THEN 1 ELSE 0 END AS gamingdate_diff,
CASE WHEN t.freeplaydownloaded <> z.freeplaydownloaded THEN 1 ELSE 0 END AS freeplaydownloaded_diff,
CASE WHEN t.freeplayadjusted <> z.freeplayadjusted THEN 1 ELSE 0 END AS freeplayadjusted_diff,
CASE WHEN t.freeplayplayed <> z.freeplayplayed THEN 1 ELSE 0 END AS freeplayplayed_diff,
CASE WHEN t.freeplayabandoned <> z.freeplayabandoned THEN 1 ELSE 0 END AS freeplayabandoned_diff,
CASE WHEN t.freeplaybalance <> z.freeplaybalance THEN 1 ELSE 0 END AS freeplaybalance_diff,
FROM testtable z
LEFT JOIN (
SELECT *
FROM freeplay.egmfreeplay
UNION ALL
SELECT *
FROM Change.EgmFreePlay
) t
ON z.slotmachinebk = t.slotmachinebk
AND z.auditdate = t.gamingdate
AND z.freeplaydownloaded = t.freeplaydownloaded
AND z.freeplayadjusted = t.freeplayadjusted
AND z.freeplayplayed = t.freeplayplayed
AND z.freeplayabandoned = t.freeplayabandoned
AND z.freeplaybalance = t.freeplaybalance
WHERE t.id IS NOT NULL -- this will only select those in 'freeplay.egmfreeplay' and 'Change.EgmFreePlay' that are not in 'testtable', I am not sure if 'id' actually exists, but you want to use something that will never be NULL in those two tables
答案 1 :(得分:0)
如果我正确理解你的问题,你需要每个表中记录不同的行...如果你有一个匹配它们的标识列,你可以加入它然后只将where子句添加到拉出那些列不同的记录。
答案 2 :(得分:0)
LEFT OUTER JOIN
我不知道关键列名称,但试试这个......
select
CASE WHEN ISNULL(t.slotmachinebk ,'')!=ISNULL(z.XXX ,'') THEN 'slotmachinebk' ELSE '' END AS slotmachinebk --<<<<wrap all columns in this, make sure you make the ISNULL alternate value match the datatype of the column
,CASE WHEN ISNULL(t.gamingdate ,'')!=ISNULL(z.gamingdate ,'') THEN 'gamingdate' ELSE '' END AS gamingdate --<<<<wrap all columns in this, make sure you make the ISNULL alternate value match the datatype of the column
,CASE WHEN ISNULL(t.freeplaydownloaded ,'')!=ISNULL(z.freeplaydownloaded ,'') THEN 'freeplaydownloaded' ELSE '' END AS freeplaydownloaded --<<<<wrap all columns in this, make sure you make the ISNULL alternate value match the datatype of the column
,CASE WHEN ISNULL(t.freeplayadjusted ,'')!=ISNULL(z.freeplayadjusted ,'') THEN 'freeplayadjusted' ELSE '' END AS freeplayadjusted --<<<<wrap all columns in this, make sure you make the ISNULL alternate value match the datatype of the column
,CASE WHEN ISNULL(t.freeplayplayed ,'')!=ISNULL(z.freeplayplayed ,'') THEN 'freeplayplayed' ELSE '' END AS freeplayplayed --<<<<wrap all columns in this, make sure you make the ISNULL alternate value match the datatype of the column
,CASE WHEN ISNULL(t.freeplayabandoned ,'')!=ISNULL(z.freeplayabandoned ,'') THEN 'freeplayabandoned' ELSE '' END AS freeplayabandoned --<<<<wrap all columns in this, make sure you make the ISNULL alternate value match the datatype of the column
,CASE WHEN ISNULL(t.freeplaybalance ,'')!=ISNULL(z.freeplaybalance ,'') THEN 'freeplaybalance' ELSE '' END AS freeplaybalance --<<<<wrap all columns in this, make sure you make the ISNULL alternate value match the datatype of the column
from (select * from freeplay.egmfreeplay union all select * from Change.EgmFreePlay) t
LEFT OUTER JOIN testtable z ON t.KEY=z.KEY --<<<<<<<<key names???
where not exists (select * from testtable where
slotmachinebk = t.slotmachinebk and
auditdate = t.gamingdate and
freeplaydownloaded = t.freeplaydownloaded and
freeplayadjusted = t.freeplayadjusted and
freeplayplayed = t.freeplayplayed and
freeplayabandoned = t.freeplayabandoned and
freeplaybalance = t.freeplaybalance
)
答案 3 :(得分:0)
select t.slotmachinebk
,t.gamingdate
, t.freeplaydownloaded
, t.freeplayadjusted
, t.freeplayplayed
, t.freeplayabandoned
, t.freeplaybalance
from (select * freeplay.egmfreeplay union all select * from Change.EgmFreePlay) t
left join testtable tt on
tt.slotmachinebk = t.slotmachinebk
and tt.auditdate = t.gamingdate
and tt.freeplaydownloaded = t.freeplaydownloaded
and freeplayadjusted = t.freeplayadjusted
and tt.freeplayplayed = t.freeplayplayed
and tt.freeplayabandoned = t.freeplayabandoned
and tt.freeplaybalance = t.freeplaybalance
where tt.whateverTheHeckMyIdColumnIs is null
这应该可以让你在你的联盟中找到与testtable中没有完全匹配的东西。当然你也不应该使用select *,尤其是不能在union中使用(这个查询会在第一次有人更改某个表结构而不是联合中的两个表时中断)。学习指定列。如果testtable上没有id,请先创建一个id,或将where子句更改为一个不能为null的字段。