没有临时表的调整结果集

时间:2018-12-14 17:23:33

标签: sql-server

enter image description here

表1(orgumcae)

caseid | auth_status_summary | delegate_system
-------+---------------------+----------------
100    | 1                   | 14
200    | 2                   | 13

SQL:

create table orgumcae(caseid int, auth_status_summary int, delegate_system int);
insert into orgumcae values(100, 1, 14);
insert into orgumcae values(200, 2, 13);

表2(字符串语言环境)

id  | subid | string
----+-------+--------
148 | 1     | AAA
148 | 2     | BBB
148 | 3     | CCC
148 | 14    | DDD
152 | 11    | WWW
152 | 12    | XXX
152 | 13    | YYY
152 | 14    | ZZZ

SQL:

create table stringlocale(id int, subid int, string varchar(40));
insert into stringlocale values(148,1,'AAA');
insert into stringlocale values(148,2,'BBB');
insert into stringlocale values(148,14,'DDD');
insert into stringlocale values(152,11,'WWW');
insert into stringlocale values(152,13,'YYY');
insert into stringlocale values(152,14,'ZZZ');

结果集

caseid | auth_status_summary |auth_status_summary_string | delegate_system | delegate_system_string
-------+---------------------+---------------------------+-----------------+-----------------------
100    | 1                   | AAA                       | 14              | ZZZ
200    | 2                   | BBB                       | 13              | YYY

有2个表Table1Table2和一个结果集。我可以在不使用临时表的情况下获得一些帮助以实现结果集吗?

auth_status_summary的{​​{1}}列表示Table1的{​​{1}}列的148值,id的{​​{1}}列表示152的值Table2中的delegate_system,因此对于Table1使用where子句idTable2使用where子句auth_status_summary

2 个答案:

答案 0 :(得分:1)

Select case_id, t1.auth_status_summary, t2.string, t1.delegate_system, t22.string 
from table1 as t1
inner join table2 as t2 on t1.auth_status_summary = t2.subid 
inner join table2 as t22 on t1.delegate_system = t22.subid

答案 1 :(得分:0)

这应该做:

select t1.caseid,
       t1.auth_status_summary,
       t2_auth.string as auth_status_summary_string,
       t1.delegate_system,
       t2_deleg.string as delegate_system_string
  from orgumcae t1
  left outer join stringlocale t2_auth
    on t2_auth.id = 148
   and t2_auth.subid = t1.auth_status_summary
  left outer join stringlocale t2_deleg
    on t2_deleg.id = 152
   and t2_deleg.subid = t1.delegate_system
 order by 1

关键部分是两次使用不同别名的Table2stringlocale)。

查看此db<>fiddle