如何汇总整体的各个部分并在SQL中显示每个部分的总和?

时间:2017-06-29 19:56:57

标签: mysql sql

这完全让我感到难过。就像问题陈述一样,我想总结一个整体的部分,然后显示每个部分的总和。更具体地说,我有几个投资组合,每个投资组合都有几个账户。我想对每个投资组合的账户数量求和,然后在每一行显示投资组合的数量,以与账户数量进行比较。我到目前为止的内容如下

select PORTFOLIO, ACCOUNT_DESCRIPTION, SECURITY, CUSIP, ACT_QTTY, 
  SUM(ACT_QTTY) as PORTFOLIO_QTTY 
from psc_filled_orders 
where TRADE_DATE_INT > 20170601 
group by PORTFOLIO, ACCOUNT_DESCRIPTION, SECURITY, CUSIP, ACT_QTTY

我读了几个关于总和的问题和关于运行总和的两个问题,但我正在寻找的不是这些。认为你可以帮助我吗?

一般来说,我希望我的结果看起来像这样:

Portfolio | Account | Account Quantity | Portfolio Quantity
-----------------------------------------------------------
Port 1    | Act A   | 300              | 700
Port 1    | Act B   | 200              | 700
Port 1    | Act C   | 200              | 700
Port 2    | Act A   | 800              | 500
Port 2    | Act B   | -300             | 500

有时会像我的例子中那样重复使用帐户名称,有时它们是唯一的,投资组合名称始终是唯一的。数量既有正面也有负面。

3 个答案:

答案 0 :(得分:2)

一种方法

SELECT a.PORTFOLIO, ACCOUNT_DESCRIPTION, ACT_QTTY, PORTFOLIO_QTTY 
  FROM (
    SELECT PORTFOLIO, ACCOUNT_DESCRIPTION, SUM(ACT_QTTY) AS ACT_QTTY 
      FROM psc_filled_orders 
     WHERE TRADE_DATE_INT > 20170601 
     GROUP BY PORTFOLIO, ACCOUNT_DESCRIPTION
) a JOIN (
  SELECT PORTFOLIO, SUM(ACT_QTTY) AS PORTFOLIO_QTTY
    FROM psc_filled_orders 
   WHERE TRADE_DATE_INT > 20170601
   GROUP BY PORTFOLIO
) p
   ON a.PORTFOLIO = p.PORTFOLIO

示例输出:

+-----------+---------------------+----------+----------------+
| PORTFOLIO | ACCOUNT_DESCRIPTION | ACT_QTTY | PORTFOLIO_QTTY |
+-----------+---------------------+----------+----------------+
| Port 1    | Act A               |      300 |            700 |
| Port 1    | Act B               |      200 |            700 |
| Port 1    | Act C               |      200 |            700 |
| Port 2    | Act A               |      800 |            500 |
| Port 2    | Act B               |     -300 |            500 |
+-----------+---------------------+----------+----------------+

你也可以考虑WITH ROLLUP,它会给你一个稍微不同的输出但是完全意味着这个(计算每个分组级别和总计的小计)

SELECT PORTFOLIO, ACCOUNT_DESCRIPTION, SUM(ACT_QTTY) AS ACT_QTTY 
  FROM psc_filled_orders 
 WHERE TRADE_DATE_INT > 20170601 
 GROUP BY PORTFOLIO, ACCOUNT_DESCRIPTION WITH ROLLUP
 HAVING PORTFOLIO IS NOT NULL -- this is to remove the grand total

哪个会给你

+-----------+---------------------+----------+
| PORTFOLIO | ACCOUNT_DESCRIPTION | ACT_QTTY |
+-----------+---------------------+----------+
| Port 1    | Act A               |      300 |
| Port 1    | Act B               |      200 |
| Port 1    | Act C               |      200 |
| Port 1    | NULL                |      700 | - this is subtotal for Port 1
| Port 2    | Act A               |      800 |
| Port 2    | Act B               |     -300 |
| Port 2    | NULL                |      500 | - this is subtotal for Port 2
+-----------+---------------------+----------+

以下是两个查询的dbfiddle演示

答案 1 :(得分:-1)

仔细检查子查询上的连接是否正确。我不确定您使用的是什么ID。

select pfo.PORTFOLIO
,pfo.ACCOUNT_DESCRIPTION
,pfo.SECURITY
,pfo.CUSIP
,pfo.ACT_QTTY
,(SELECT SUM(ACT_QTTY) FROM psc_filled_orders where pfo.cusip = cusip and TRADE_DATE_INT > 20170601) as PORTFOLIO_QTTY 
from psc_filled_orders pfo
where pfo.TRADE_DATE_INT > 20170601 
group by pfo.PORTFOLIO, pfo.ACCOUNT_DESCRIPTION, pfo.SECURITY, pfo.CUSIP, pfo.ACT_QTTY

答案 2 :(得分:-1)

试试这个 选择PORTFOLIO,ACCOUNT_DESCRIPTION,SECURITY,CUSIP,ACT_QTTY,   SUM(ACT_QTTY)OVER(PORTFOLIO分区)为PORTFOLIO_QTTY 来自psc_filled_orders 其中TRADE_DATE_INT> 20170601