之前已经讨论过这个问题,但是没有一个答案解决了我的具体问题,因为我正在处理内部和外部选择中的不同where子句.此查询在Sybase下执行得很好,但在SQL Server下执行时会在此帖子的标题中给出错误.查询很复杂,但查询的一般大纲是:
select sum ( t.graduates -
( select sum ( t1.graduates )
from table as t1
where t1.id = t.id and t1.group_code not in ('total', 'others' ) ) )
from table as t
where t.group_code = 'total'
Run Code Online (Sandbox Code Playgroud)
以下描述了我要解决的情况:
无论如何使用派生表或联接来重写它以获得相同的结果?
更新:我为我的具体问题创建了样本数据和3个解决方案(2个受sgeddes影响).我添加的那个涉及将相关子查询移动到FROM子句中的派生表.谢谢你的帮助!