2009-06-11 127 views
0
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查詢,給我的記錄不匹配測試表和freeplay.egmfreeplay ....但我怎麼修改這個查詢來獲取不匹配的列名/值,列名是在子查詢與運算使用的列...我如何獲得列名和/或值?

+0

請縮進您的代碼,現在很難閱讀。 – RedFilter 2009-06-11 18:38:44

+0

列出了將egmfreeplay加入測試表 – 2009-06-11 18:41:50

回答

3

你可以放了一堆的「CASE WHEN」在你的SELECT查詢語句

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 
+0

的列,這不起作用。查詢中只有一個表:「t」,其中的子查詢中的值不能用於where。您需要加入其他表格,然後使用CASE WHEN。不要忘記佔NULL NULL – 2009-06-11 18:51:22

+1

我試過了,但它給了我「多部分標識符無法綁定」錯誤 ,因爲我會使用子查詢的列(其中提及所有的AND)與列進行比較主要查詢。由於子查詢中的columsn在範圍之外使用,它給了我錯誤...儘管 – sagar 2009-06-11 18:54:00

+0

我以前的評論是針對Jon ... – sagar 2009-06-11 18:55:12

0

如果我正確理解你的問題,你想從每個錶行,其中記錄是不同的...如果你有一個標識列,以配合他們,你可以加入對,然後添加你的where子句只提取那些列不同的記錄。

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 
        ) 
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 

這應該讓你無論是在你的工會在測試表中沒有完全匹配。當然,你不應該使用select *,尤其是不能使用select *,尤其是不能使用select *(這個查詢會在第一次改變某個表結構時破壞聯合中的表而不是兩個表)。學習指定你的專欄。如果您在測試表上沒有標識,請先創建一個或將where子句更改爲不能爲空的字段。