0

次のようなSQL更新クエリがあります。

update #tree c 
set treetop=p.treetop,
treeptag=p.treeptag,
adjust= 'a2a'
from #tree p ,#regions r 
where c.treeptag=''
and   c.xsreg=r.Region
and    c.xsreg <> c.reg
and c.tradedate=p.tradedate
and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')

次のように、結合を使用して「fromlist」を使用せずにクエリを作成しようとしています。

update #tree c 
set treetop=(select p.treetop from #tree p ,#regions r 
                      where c.treeptag=''
                      and   c.xsreg=r.Region
                      and    c.xsreg <> c.reg
                      and c.tradedate=p.tradedate
              and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')),
treeptag=(select p.treeptag from #tree p ,#regions r 
                      where c.treeptag=''
                      and   c.xsreg=r.Region
                      and    c.xsreg <> c.reg
                      and c.tradedate=p.tradedate
              and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')),
adjust= 'a2a'
where exists (select 1 from #tree p ,#regions r  where c.treeptag=''
and   c.xsreg=r.Region
and   c.xsreg <> c.reg
and c.tradedate=p.tradedate
and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ',''))

ただし、上記のクエリは間違った行数を返すようです。どんな提案でも最も役に立ちます。

4

1 に答える 1

0

変換は正しいので、行数は完全に同じである必要があります。テーブルが小さい場合は、質問をDDLとサンプルデータの挿入で更新します。そうでなければ、これらは何を返しますか?

select count(1)
from #tree c, #tree p ,#regions r 
where c.treeptag=''
and   c.xsreg=r.Region
and    c.xsreg <> c.reg
and c.tradedate=p.tradedate
and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')

select count(disctint c.id)  -- or whatever the unique key on #tree is
from #tree c, #tree p ,#regions r 
where c.treeptag=''
and   c.xsreg=r.Region
and    c.xsreg <> c.reg
and c.tradedate=p.tradedate
and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')

そして、両方のカウントはアップデートとどのように比較されますか?2番目のカウントと一致すると思いました。

価値があるのは2012年なので、適切なANSI結合を使用します。

update c 
   set treetop=p.treetop, treeptag=p.treeptag, adjust='a2a'
  from #tree c
  join #tree p on c.tradedate=p.tradedate
  join #regions r on c.xsreg=r.Region 
 where c.treeptag='' and c.xsreg <> c.reg
   and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')

そして、あなたの条件のいくつかは直接反対cです、それでなぜそれらをサブクエリに巻き込むのですか?

update #tree c
   set treetop =(select p.treetop
                   from #tree p
                   join #regions r on c.xsreg=r.Region
                  where c.tradedate=p.tradedate
                    and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')),
       treeptag=(select p.treeptag
                   from #tree p
                   join #regions r on c.xsreg=r.Region 
                  where c.tradedate=p.tradedate
                    and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')),
       adjust  ='a2a'
 where c.treeptag='' and c.xsreg <> c.reg and -- << these two
exists (select 1
          from #tree p
          join #regions r on c.xsreg=r.Region
         where c.tradedate=p.tradedate
           and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ',''))
于 2012-10-07T08:43:55.020 に答える