简体   繁体   English

如何获取返回总和的子查询的总和?

[英]How to get the sum of a subquery that returns sums?

I would like to get the sum of the "sums" in the following case.我想在以下情况下获得“总和”的总和。 How can I do that?我怎样才能做到这一点? The following picture represents the table I receive after performing the code below.下图代表了我执行下面的代码后得到的表格。 I'd like to receive only 2 records which would be:我只想收到 2 条记录,它们是:

Or Gazit |或 Gazit | 052-3322445 | 052-3322445 | 36400 36400

Dan Guy |丹盖 | 050-3023322 | 050-3023322 | 12000 12000

代表我得到的表格的图像

SELECT Distinct(o1.FirstName & " " & o1.LastName) as [Full Name], o1.PhoneStart & "-" & 
o1.PhoneNumber as [Phone Number],
(SELECT SUM(OrderProducts.ClientPrice*OrderProducts.Quantity) FROM OrderProducts WHERE 
OrderProducts.Order = o2.ID AND o2.Client = o1.ID) AS [Order Price]

FROM Clients as o1 INNER JOIN (Orders as o2 INNER JOIN OrderProducts as o3 ON o2.ID = 
o3.Order) ON o1.ID = o2.Client
ORDER BY 3 DESC;

Thanks.谢谢。

Either I am misunderstanding your requirement or you are over-complicating this.要么我误解了您的要求,要么您将其复杂化。 Seems like you just need some aggregation (ie sum) with a group by clause.似乎您只需要使用 group by 子句进行一些聚合(即总和)。 Does this work for you?这对你有用吗?

select o1.FirstName & " " & o1.LastName     as [Full Name]
     , o1.PhoneStart & "-" & o1.PhoneNumber as [Phone Number]
     , sum(o3.ClientPrice * o3.Quantity)    as [Order Price]
from Clients o1
inner join Orders o2        on o1.ID = o2.Client
inner join OrderProducts o3 on o2.ID = o3.Order
group by o1.FirstName
       , o1.LastName
       , o1.PhoneStart
       , o1.PhoneNumber
order by 3 desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM