将 Oracle 查询转换为 SQL Server - 错误 130
我的查询是这样的:
select u1.LastName, u1.FirstName,
sum(case when not exists(select 1
from CaseHistory ch2
where ch1.caseid = ch2.caseid
and ch2.ActiveDate = somedate
ch2.ID in (0, 2, 41))
then 1
else 0
end) as WorkedRecord
from sometables
此查询在 Oracle 中有效,但我们现在正在切换到 SQL Server。在 SQL Server 中,我们收到以下错误:
错误 130:无法对包含聚合或聚合的表达式执行聚合函数 子查询。
这是我的解决方案:
我最终做了一个内联视图,然后对其进行总结。我做了这样的事情: 选择总和(a.WorkedRecord) 从 (选择 (不存在的情况(选择1 来自案例历史第 2 章... then 1 else 0 end) 作为 WorkedRecord) 来自 Sometables)a
这似乎有效。
The query I have is this:
select u1.LastName, u1.FirstName,
sum(case when not exists(select 1
from CaseHistory ch2
where ch1.caseid = ch2.caseid
and ch2.ActiveDate = somedate
ch2.ID in (0, 2, 41))
then 1
else 0
end) as WorkedRecord
from sometables
This query works in Oracle, but we're now changing over to SQL Server. In SQL Server we get the following error:
Error 130: Cannot perform an aggregate function on an expression containing an aggregate or
a subquery.
Here's my solution:
I ended up doing an in-line view and then summing that. I did something like this:
select sum(a.WorkedRecord)
from
(select
(case when not exists(select 1
from CaseHistory ch2...
then 1 else 0 end) as WorkedRecord)
from Sometables) a
This seems to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,您可以执行
左连接
,而不是子查询,然后对连接结果求和吗?Instead of the sub-query... could you do a
left join
in this situation, and then total the results of the join?