Aggregate/Sum 子查询上的函数

Aggregate/Sum function on a subquery

我在查询(子查询?)的输出中遇到问题 aggregating/summing。这是我的初始代码和输出:

代码:

SET ARITHABORT OFF 
SET ANSI_WARNINGS OFF
SELECT
AsOfDate,
round(ColA/sum(ColB) * (ColC),4) As 'Monthly_Return'
from xyz.abc
WHERE
AsOfDate BETWEEN '2021-05-25' AND '2021-05-26'
and ColD like '123%'
GROUP BY AsOfDate, ColA,ColB,ColC



|   AsOfDate |MarketV(ColA)|MarketV(ColB)|Return(ColC)|(ColD)|
|------------|-------------|-------------|------------|------|
| 2021-05-25 |   8891171.14|   8891171.14| -0.81166911|   123|
| 2021-05-25 |  14219713.92|  14219713.92|  2.12135625|   123|
| 2021-05-25 |   3102248.42|   3102248.42|  0.36907554|   123|
| 2021-05-26 |   8819004.25|   8819004.25|  0.88822511|   123|
| 2021-05-26 |  14521364.71|  14521364.71|  1.84104756|   123|
| 2021-05-26 |   3113698.06|   3113698.06|  0.44211577|   123|

|The sum of ColB for 2021-05-25 is 26,213,133.48|


|The sum of ColB for 2021-05-25 is 26,454,067.02|

我想要 ColAi/ColB * ColCi 按日期分组的总和。

|For 2021-05-25                              |  Return |
|--------------------------------------------|---------|
|8891171.14 / 26,213,133.48 * -0.81166911   =|-0.275308|
|14219713.92 / 26,213,133.48 * 2.12135625   =|1.150762 |
|3102248.42 / 26,213,133.48 * 0.36907554    =|0.043679 |

|For 2021-05-26                              |  Return |
|--------------------------------------------|---------|
|8819004.25 / 26,454,067.02 * 0.88822511    =|0.296108 |
|14521364.71 / 26,454,067.02 * 1.84104756   =|1.0106016|
|3113698.06 / 26,454,067.02 * 0.44211577    =|0.0520379|

我当前的代码显示以下输出:

截止日期 Monthly_Return
2021-05-25 -0.275308
2021-05-25 1.150762
2021-05-25 0.043679
2021-05-26 0.296108
2021-05-26 1.0106016
2021-05-26 0.0520379

我真正想要的是:

截止日期 Monthly_Return
2021-05-25 0.9191
2021-05-26 1.3587

根据您的新要求和新列名完全重写。 (对于它的价值,因为你已经选择了一个答案。)

with a as (
  select AsOfDate
  , ColA
  , sum(ColB) over (partition by AsOfDate) as ColB
  , ColC
  from abc
  where AsOfDate BETWEEN '2021-05-25' AND '2021-05-26'
    and ColD like '123%'
)

select AsOfDate
, cast(round(sum(ColA * ColC / ColB), 4) as decimal(6,4)) as 'Monthly_Return'
from a
group by AsOfDate
;

也许下次,在您的问题中包含一个 dbfiddle,以便您的结构和数据可用。此外,在您首次提出问题时,请包括您期望的结果。

如果查询真的给了你想要的子结果,那么你所要做的就是将查询放在子查询中并再次聚合:

SELECT
  asofdate,
  SUM(monthly_return)
FROM ( <your query here> ) subquery
GROUP BY asofdate
ORDER BY asofdate;

由于很少有人按所选的所有列进行分组,因此我向您展示了一些示例数据和结果,因此您可以仔细检查:

有了这个数据

AsOfDate ColumnA ColumnB ColumnC ColumnD
2021-05-25 12 2 3 123
2021-05-25 12 2 3 123
2021-05-25 12 2 3 123
2021-05-25 12 1 3 123
2021-05-25 12 1 3 123
2021-05-25 12 1 3 123

您的查询将得到

AsOfDate Monthly_Return
2021-05-25 6
2021-05-25 12

最后的聚合使得这个

AsOfDate SUM(Monthly_Return)
2021-05-25 18

演示:https://dbfiddle.uk/?rdbms=postgres_13&fiddle=3e2907d96baa1c73e29f223c0cf7edc2

更新

根据您上次的编辑,您只需要:

SELECT asofdate, SUM(columna * columnc) * 1.0 / SUM(columnb)
FROM xyz.abc
WHERE asofdate BETWEEN '2021-05-25' AND '2021-05-26'
AND columnd LIKE '123%'
GROUP BY asofdate
ORDER BY asofdate;

演示:https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=a2b40e9cb83a22c8cc5831b16e61ffca

在 SQL 服务器中可能需要与 1.0 相乘,否则它可能不适当地应用整数除法(取决于您的数据类型)。

如果您想使用中间结果,这里是您的查询更正:

SELECT
  asofdate,
  ROUND(cola * 1.0 / SUM(colb) OVER (PARTITION BY asofdate) * colc, 4) AS monthly_return
from xyz.abc
WHERE asofdate BETWEEN '2021-05-25' AND '2021-05-26'
AND cold LIKE '123%';

如前所述,您可以在子查询中使用它来获得最终结果。

演示:https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=82b3977d5b0591fe0a2b5bc65b3dab34