简体   繁体   English

订单项表的总和SQL子查询

[英]Sum of sums from tables of line items SQL Subquery

I have 3 tables: CUSTOMERS CLOSEDORDERS CLOSEDORDERARCHIVES 我有3张桌子:客户壁橱CLOSEDORDERARCHIVES

Each is linked by a customer ID field. 每个都由客户ID字段链接。 CLOSEDORDERS and CLOSEDORDERSARCHIVES are tables with every line item of every invoice ever sold. CLOSEDORDERS和CLOSEDORDERSARCHIVES是包含已售出的每张发票的每个行项目的表。 I'm trying to come up with lifetime totals for the customers, but I'm stuck on the final sum. 我正在尝试为客户提供终生总计,但是我被困在最后的金额上。 Here is what I have written, it outputs correct totals for each table in two columns (some of the values are null): 这是我写的内容,它在两列中为每个表输出正确的总计(某些值为空):

SELECT CUSTOMERS.Company, CUSTOMERS.[Ship City], CUSTOMERS.[Ship State], 
      (SELECT SUM (CLOSEDORDERSARCHIVE.Quantity*CLOSEDORDERSARCHIVE.SellPrice) 
      FROM CLOSEDORDERSARCHIVE 
      WHERE CLOSEDORDERSARCHIVE.CustomerID = CUSTOMERS.ID) AS archiveTotal, 
            (SELECT SUM (CLOSEDORDERS.Quantity*CLOSEDORDERS.SellPrice)
            FROM CLOSEDORDERS
            WHERE CLOSEDORDERS.CustomerID = CUSTOMERS.ID) AS currentTotal
FROM CUSTOMERS, CLOSEDORDERSARCHIVE, CLOSEDORDERS
WHERE (((CUSTOMERS.Branch)=33));

When I search, I find this answer that seems similar, but I can't make it work, is it because the sums are summed from line items in the tables and grouped by customer? 当我搜索时,我发现这个答案似乎很相似,但我无法使它起作用,是因为总和是从表中的行项目中求和并按客户分组的? Also, this is one of my first queries, is there a more efficient way to write/accomplish this? 另外,这是我的第一个查询,有没有更有效的方式编写/完成此查询?

Sum of sums in different tables 不同表中的总和

We can break this into a simpler problem first: Put everything you need in one table. 我们可以先将其分解为一个更简单的问题:将所需的所有内容放在一张表中。

If the only columns required for the sum are the quantities and sellprices (and the ID of course) you can union them in a subquery and calculate the total of that later. 如果总和所需的唯一列是数量和售价(当然还有ID),则可以在子查询中合并它们,并在以后计算总和。 The subquery will look something like: 子查询将类似于:

select CustomerID as 'ID', Quantity as 'Quantity', SellPrice as 'SellPrice'
from CLOSEDORDERSARCHIVE

UNION ALL

select CustomerID as 'ID', Quantity as 'Quantity', SellPrice as 'SellPrice'
from CLOSEDORDERS

After that, you can select from that subquery and group by customer ID and you'll get the sum totals for each customer: 之后,您可以从该子查询中选择并按客户ID进行分组,您将获得每个客户的总和:

select ID, SUM(Quantity*SellPrice) from(

select CustomerID as 'ID', Quantity as 'Quantity', SellPrice as 'SellPrice'
from CLOSEDORDERSARCHIVE

UNION ALL

select CustomerID as 'ID', Quantity as 'Quantity', SellPrice as 'SellPrice'
from CLOSEDORDERS
) as subq1
GROUP By ID

And I think that's what you need. 我认为这就是您所需要的。 You can even join the last query with the table Customers if you want the customer name and stuff like that to appear =) 如果您想显示客户名称和类似内容,甚至可以将与客户表一起的最后一个查询加入=)

I think least unintuitive solution is to join on subqueries (ugh!). 我认为最不直观的解决方案是加入子查询(哦!)。 Here's an (oversimplified, see caveat below) example: 这是一个(过于简化,请参见下面的警告)示例:

SELECT
  c.Company,
  c.[Ship City],
  c.[Ship State],
  co.Total + coa.Total AS 'Grand Total'
FROM CUSTOMERS c
LEFT OUTER JOIN (
  SELECT CustomerID, SUM(Quantity*SellPrice) as 'Total'
  FROM CLOSEDORDERS
  GROUP BY CustomerID
) AS 'co' ON c.ID = co.CustomerID
LEFT OUTER JOIN (
  SELECT CustomerID, SUM(Quantity*SellPrice) as 'Total'
  FROM CLOSEDORDERSARCHIVE
  GROUP BY CustomerID
) AS 'coa' ON c.ID = coa.CustomerID
WHERE c.Branch = 33

In this case, we're creating two intermediate tables (co and coa) which hold CLOSEDORDERS's and CLOSEDORDERSARCHIVE's order totals for each customer listed in the table. 在这种情况下,我们将创建两个中间表(co和coa),其中包含表中列出的每个客户的CLOSEDORDERS和CLOSEDORDERSARCHIVE的订单总额。

Caveat: if there are no orders in CLOSEDORDERS or CLOSEDORDERSARCHIVE for a company, than co.Total (or coa.Total) will be NULL for that company. 注意:如果公司的CLOSEDORDERS或CLOSEDORDERSARCHIVE中没有订单,则该公司的co.Total(或coa.Total)将为NULL。 You need to check for this or the addition will be wrong (IIRC NULL + a number = NULL). 您需要检查这一点,否则加法将是错误的(IIRC NULL +一个数字= NULL)。 So the "co.Total" and "coa.Total" expressions of the SELECT clause should use a COALESCE or CASE statement to check for it. 因此,SELECT子句的“ co.Total”和“ coa.Total”表达式应使用COALESCE或CASE语句进行检查。 Something like: 就像是:

...COALESCE(co.Total,0) + COALESCE(coa.Total,0) AS 'Grand Total'...

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

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