SQL Server“无法对包含聚合或子查询的表达式执行聚合函数”,但S
发布时间:2020-12-12 16:32:18 所属栏目:MsSql教程 来源:网络整理
导读:这个问题已经讨论了,但是没有一个答案解决我的具体问题,因为我正在处理内部和外部选择的不同的where子句.此查询在Sybase下执行得很好,但在SQL Server下执行时,会发出此帖子的标题中的错误.查询很复杂,但查询的一般大纲是: select sum ( t.graduates - ( sele
这个问题已经讨论了,但是没有一个答案解决我的具体问题,因为我正在处理内部和外部选择的不同的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' 以下描述我正在解决的情况: >所有组代码代表比赛,除了“总”和“其他” 有没有使用派生表或联接来重写这个来获得相同的结果? 更新:我创建了sample data and 3 solutions to my specific problem(2受sgeddes影响).我添加的包括将相关子查询移动到FROM子句中的派生表.谢谢你的帮助! 解决方法一个选项是将子查询置于LEFT JOIN中:select sum ( t.graduates ) - t1.summedGraduates from table as t left join ( select sum ( graduates ) summedGraduates,id from table where group_code not in ('total','others' ) group by id ) t1 on t.id = t1.id where t.group_code = 'total' group by t1.summedGraduates 也许更好的选择是使用SUM与CASE: select sum(case when group_code = 'total' then graduates end) - sum(case when group_code not in ('total','others') then graduates end) from yourtable SQL Fiddle Demo with both (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
热点阅读