无法对子查询执行聚合函数
有人可以帮我解决这个问题吗?
SELECT p.OwnerName, SUM(ru.MonthlyRent) AS PotentinalRent, SUM(
(SELECT COUNT(t.ID) * ru.MonthlyRent FROM tblTenant t
WHERE t.UnitID = ru.ID)
) AS ExpectedRent
FROM tblRentalUnit ru
LEFT JOIN tblProperty p ON p.ID = ru.PropertyID
GROUP BY p.OwnerName
我在处理第二笔款项时遇到了问题,它不允许我这样做。显然 SUM 不适用于子查询,但我需要计算预期租金(如果有租户分配给 RentalUnit 的 id,则为 MonthlyRent,其中 0 个不是)。我怎样才能做到这一点?
Can someone help me with this query?
SELECT p.OwnerName, SUM(ru.MonthlyRent) AS PotentinalRent, SUM(
(SELECT COUNT(t.ID) * ru.MonthlyRent FROM tblTenant t
WHERE t.UnitID = ru.ID)
) AS ExpectedRent
FROM tblRentalUnit ru
LEFT JOIN tblProperty p ON p.ID = ru.PropertyID
GROUP BY p.OwnerName
I'm having problems with the second sum, it won't let me do it. Evidently SUM won't work on subqueries, but I need to calculate the expected rent (MonthlyRent if there is a tenant assigned to the RentalUnit's id, 0 of they're not). How can I make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个要检查的测试脚本:
Here's a test script to check:
对于某些特定的租赁单位 (
COUNT(t.ID) * ru.MonthlyRent
),unitMonthlyRent 乘以租户数量的总和意味着什么?您是否只想查看所有单位的潜在总租金与预期租金(仅使用的单位)之间的差异?如果是这样,那么试试这个
What is the meaning of the sum of the unitMonthlyRent times the number of tenants, for some partiicular rental unit (
COUNT(t.ID) * ru.MonthlyRent
)?Is it the case that all you are trying to do is see the difference between the total potential rent from all untis versus the expected rent (From only occcupied units) ? If so, then try this