简体   繁体   English

SQL中SUM的求和

[英]SUMs by SUMs in SQL

I have two tables, one representing securities ownership (where there may be more than one holding in each security) and one representing distributions on each security (where there may be more than one distribution on each). 我有两个表,一个表代表证券所有权(每个证券中可能有多个控股),另一个表代表每个证券的分布(每个证券中可能有多个分配)。 I would like to compute total distribution paid on each security. 我想计算每种证券支付的总发行额。

Thus, given input like 因此,给定输入像

Security ID    shares held
 44                100
 44                100
 45                200
 55                300


Security ID    distribution
 44               0.05
 45               0.06
 55               0.07
 55               0.03
 44               0.05

The output should be 输出应为

Security ID    total distribution
 44               20
 45               12
 55               30

The problem is, SUM(A.sharecount)*SUM(B.distribution) ends up double counting some of the positions and I get answers that are too big [(80, 12, 60) instead of (20, 12, 30) in the example above]. 问题是,SUM(A.sharecount)* SUM(B.distribution)最终重复计算了一些头寸,而我得到的答案太大[(80,12,60)而不是(20,12,30)在上面的示例中]。

SQLFiddle with my sample input and failing SQL can be seen at http://sqlfiddle.com/#!9/e3caa/3/0 可以在http://sqlfiddle.com/#!9/e3caa/3/0上看到带有我的示例输入和失败SQL的SQLFiddle

All advice appreciated. 所有建议表示赞赏。

This works as requested: 这可以按要求工作:

SELECT
  S.instrumentID,
  S.shares,
  D.distribution,
  S.shares * D.distribution as total
FROM (
  SELECT instrumentID, SUM(sharecount) shares
  FROM samplePortfolio
  GROUP BY instrumentID
) S
JOIN (
  SELECT instrumentID, SUM(distribution) distribution
  FROM sampleDistributions
  GROUP BY instrumentID
) D USING(instrumentID)

SQL Fiddle SQL小提琴

It works by pre-computing the total number of shares and the total distribution in separate subqueries, and then joins them together to make the product, giving the correct value. 它通过预先计算单独的子查询中的股份总数和总分配,然后将它们连接在一起以制造产品,从而给出正确的值来工作。

Simplest fix: 最简单的解决方法:

SELECT
  A.instrumentID,
  SUM(A.sharecount * B.distribution)
FROM
    samplePortfolio A
INNER JOIN
    sampleDistributions B
ON
    A.instrumentID=B.instrumentID
GROUP BY
    A.instrumentID

The reason this is a fix is that the join matches every row from A with every row from B. If you sum across A, then sum across B, then multiply, you double-count for the rows that got duplicated during the join. 究其原因,这是一个修复的是,从A连接匹配的每一行与每一 B.行如果您在一笔,然后在乙总结,再乘,您双击计数是在加入得到了重复的行。 If you multiply, then sum, you are multiplying rows only with their "partner" rows. 如果相乘,然后相加,则仅将行与它们的“伙伴”行相乘。

Problems coming from double-counting join results are one of the most common errors I see in SQL, so be on the lookout! 重复计算联接结果带来的问题是我在SQL中看到的最常见的错误之一,所以要当心了!

SQL Fiddle: http://sqlfiddle.com/#!9/e3caa/14 SQL小提琴: http ://sqlfiddle.com/#!9 / e3caa / 14

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

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