Как я могу получить имя столбца и или значение? - PullRequest
0 голосов
/ 11 июня 2009
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 .... но как мне изменить этот запрос, чтобы получить имя / значение столбца, которые не совпадают, имена столбцов являются столбцами используется в подзапросе ANDs ...

Ответы [ 4 ]

3 голосов
/ 11 июня 2009

Вы можете поместить несколько операторов 'CASE WHEN' в ваш запрос SELECT

CASE WHEN a.column1 <> b.column1 THEN 1 ELSE 0 END AS column1_diff

Тогда вы увидите в своем наборе результатов «1», если этот столбец отличается для этой записи, или «0», если он не отличается

EDIT

Я попытался реорганизовать ваш запрос, чтобы он работал.

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 голосов
/ 11 июня 2009
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 *, особенно не в union (этот запрос сломался бы при первом изменении структуры таблицы в одной, но не в обеих таблицах объединения). Научитесь указывать свои столбцы. Если у вас нет идентификатора в testtable, сначала создайте его или замените условие where на поле, которое никогда не может быть пустым.

0 голосов
/ 11 июня 2009

ВЛЕВО НАРУЖНОЕ СОЕДИНЕНИЕ

Я бы не знал названия ключевых столбцов, но попробуйте это ...

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 голосов
/ 11 июня 2009

Если я правильно понимаю ваш вопрос, вам нужны строки из каждой таблицы, в которых записи различаются ... если у вас есть столбец идентификаторов, чтобы сопоставить их, вы можете присоединиться к этому, а затем добавить свое предложение where только к вытащить записи, где эти столбцы разные.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...