SQL SERVER中的内部联接与CASE语句不起作用

SQL SERVER中的内部联接与CASE语句不起作用,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有两个表的账户代码如下 table 1: account 50000 50006 50015 50105 50150 50155 50165 table 2: Account 50000 50010 50140 50105 50150 50155 50165 我需要连接这两个表。如果表1的任何科目代码与表2不匹配,则我已将表1科目代码隐式更改为表2科目代码 我做了如下的事情 SELECT T1.Account, T2.Account FROM table1 t1 INN

我有两个
的账户代码如下

table 1:

account  

50000
50006
50015
50105
50150
50155
50165

table 2:

Account

50000
50010
50140
50105
50150
50155
50165
我需要连接这两个
。如果
表1
的任何科目代码与
表2
不匹配,则我已将表1科目代码隐式更改为表2科目代码

我做了如下的事情

 SELECT T1.Account, T2.Account
 FROM table1 t1 
 INNER JOIN table2 t2
 on (t2.account =  CASE t1 .account 
                   WHEN 50015 THEN 50010
                   WHEN 50006 THEN 50140
                   ELSE  t1 .account 
                    END )
但我只得到了匹配的代码作为输出

account Account
50000   50000
50105   50105
50150   50150
50155   50155
50165   50165
我没有得到不匹配的帐户代码,即
(50006和50015)
。有人能帮我找出这里出了什么问题吗

我的预期产出是

account Account
50000   50000
50006   50140
50015   50010
50105   50105
50150   50150
50155   50155
50165   50165
谢谢你的帮助

试试这个


使用
CASE
然后使用
DISTINCT
数据,这将为您提供通用解决方案

-- table1
declare  @table1 table
(account  bigint)

insert into @table1 values (50000)
insert into @table1 values (50006)
insert into @table1 values (50015)
insert into @table1 values (50105)
insert into @table1 values (50150)
insert into @table1 values (50155)
insert into @table1 values (50165)

-- table2
declare  @table2 table
(account  bigint)

insert into @table2 values (50000)
insert into @table2 values (50010)
insert into @table2 values (50140)
insert into @table2 values (50105)
insert into @table2 values (50150)
insert into @table2 values (50155)
insert into @table2 values (50165)


-- QUERY
select distinct t1.account as Account1, 
Account2 = case 
    when  t1.account = t2.account then t2.account else  t1.account
    end
from @table1 t1, @table2 t2
结果

Account1    Account2
50000       50000
50006       50006
50015       50015
50105       50105
50150       50150
50155       50155
50165       50165
Account1    Account2
50000       50000
50006       50140
50015       50010
50105       50105
50150       50150
50155       50155
50165       50165
评论后编辑——这是我们要求的一部分。我需要更新50140中50006账户代码对应的金额,依此类推

结果

Account1    Account2
50000       50000
50006       50006
50015       50015
50105       50105
50150       50150
50155       50155
50165       50165
Account1    Account2
50000       50000
50006       50140
50015       50010
50105       50105
50150       50150
50155       50155
50165       50165
你可以试试这个

   SELECT T1.Account, T2.Account
   FROM table1 t1 
   INNER JOIN table2 t2
   on (t2.account =  CASE 
               WHEN t1.account = 50015 THEN 50010
               WHEN t1.account=50006 THEN 50140
               ELSE  t1.account 
                END )

它应该按索引吗?表1中的第一个应与表2中的第一个核对,等等?您的问题令人困惑。匹配的帐户是明确的,但如何才能找到不匹配的帐户?其他表中不存在的任何帐号都是不匹配记录。这些表中还有其他字段吗?帐户之间的映射是什么?我尝试了你的代码和它的工作方式请参见@FLICKER,如果发现任何不匹配的帐户,我必须将其转换为匹配的帐户“如果发现任何不匹配的帐户,我必须将其转换为匹配的帐户”是什么意思?如果你能把你的预期结果包括进去,那就更清楚了。那句话毫无意义。如果不匹配,就不匹配。我们只能假设,当数字不匹配时,魔术决定了数字应该是什么。
SELECT T1.Account, T2.Account,(CASE t1 .account 
                   WHEN 50015 THEN 50010
                   WHEN 50006 THEN 50140
                   ELSE  t1 .account 
                    END) as New_Account
 FROM table1 t1 
INNER JOIN table2 t2
 on (t2.account =  t1.account )