Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 不带临时表的调整结果集_Sql Server - Fatal编程技术网

Sql server 不带临时表的调整结果集

Sql server 不带临时表的调整结果集,sql-server,Sql Server,表1组织 SQL: 表2:现场环境 id |子id |字符串 --+----+---- 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: 结果集 caseid |授权状态|摘要|授权状态|摘要|字符串|委托|系统|委托|系统|字符串 ----+-----------+--------------+---

表1组织

SQL:

表2:现场环境

id |子id |字符串 --+----+---- 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:

结果集

caseid |授权状态|摘要|授权状态|摘要|字符串|委托|系统|委托|系统|字符串 ----+-----------+--------------+---------+------------ 100 | 1 | AAA | 14 | ZZZ 200 | 2 | BBB | 13 | YYY 有两个表Table1和Table2以及一个结果集。在不使用临时表的情况下,我可以获得一些帮助来实现结果集吗

在这里,表1的auth_status_summary列表示表2的列id的148值,表1的delegate_system列表示表2的列id的152值,因此对于auth_status_summary,使用where子句Table2.id=148表示delegate_系统,使用where子句Table2.id=152应执行以下操作:

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
关键部分是使用两次具有不同别名的Table2 stringlocale


参见此

不清楚表之间是如何相互关联的,外键引用是如何定义的,预期的逻辑是什么,这导致了您显示的结果。另外,您的示例不可读-请尝试在问题中使用代码块。此处没有外键引用。身份验证状态摘要和委托系统都引用子ID。授权状态摘要由值148表示,委托系统由值152表示,值152由管道| caseid |授权状态摘要|委托系统100 | 1 | 14200 | 2 | 13表2 id |子id |字符串148 | 1 | AAA 148 | 2 | BBB 148 3 | CCC 148 14 | DDD | 152 | 12 | 152 |ZZZ结果集caseid | auth|u status | u summary | auth|u status | u summary | string | delegate|u system | delegate|u system | string 100 | 1 | AAA | 14 | ZZZ 200 | 2 | BBB | 13 | yyyyyyyy您可以在您的sql创建表组织中运行以下查询,委托\系统int插入到组织值100,1,14插入到组织值200,2,13创建表stringlocaleid int,subid int,字符串varchar40插入到stringlocale值148,1,'AAA'插入到stringlocale值148,2,'BBB'插入到stringlocale值148,14,'DDD'插入到stringlocale值152,11,“WWW”插入到stringlocale值152,13,“YYY”插入到stringlocale值152,14,“ZZZ”这并不是我的意思。请编辑您的原始问题,并确保您正确使用了标记语法,以使数据可读和/或提供有关重新创建现有内容的SQL说明,即CREATE TABLE和INSERT语句也使用了正确的标记。注释用于确定哪些内容不清楚以及如何不清楚,并且应通过编辑问题或答案进行澄清,除非从注释中可以看出问题一开始就很清楚。虽然此答案可能解决OP的问题,但建议您解释其工作原理以及解决问题的原因。这有助于新开发人员了解正在发生的事情,以及如何自行解决此问题和类似问题。谢谢你的贡献!当subid与两个不同的id相同时,这不起作用。drop table orgumcae create table orgumcaecaseid int、auth_status_summary int、delegate_system int插入到组织值100,3,14插入到组织值200,2,13 drop table stringlocale create table stringlocaleid int、subid int、,字符串varchar40插入到stringlocale值148,2,'BBB'插入到stringlocale值148,3,'CCC'插入到stringlocale值148,14,'DDD'插入到stringlocale值152,13,'YYY'插入到stringlocale值152,14,'ZZZ'感谢您的初始启动。选择caseid,t1.auth_status_summary,t2.string作为auth_status_summary_string,t1.delegate_system,t22.string作为委托\系统\来自orgumcae的字符串作为t1内部连接stringlocale作为t1上的t2.auth\ u status\ u summary=t2.subid和t2.id=148内部连接stringlocale作为t1上的t22.delegate\系统=t22.subid和t22.id=152Caleb Kleveter-这是旧的数据模型,其中有一个单独的表“Table2”,其“id”值表示另一个表中的列桌子身份验证状态摘要和委托系统都引用子ID。auth_status_summary由值148表示,delegate_system由值152OP表示,Op只需要输出2行,但在查询中有3行。
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);
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');
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
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