计算SQL中表的子集的总和/平均值

时间:2016-03-30 00:41:33

标签: sql sql-server sum

我正在寻找一些SQL专业知识。我正在计算平均值,首先汇总所有相关行,然后除以查询中的天数。

我无法使我的总和正常工作(所有行的总和为0)。有什么建议?谢谢!

CREATE TABLE [dbo].[intradayOHLC_Selected_Closing_Prices_Staging] (
[Ticker] varchar(50),
[Date] float,
[Date 250 Days Ago] float,
[Close] float
)

INSERT INTO [intradayOHLC_Selected_Closing_Prices_Staging] ([Ticker], [Date])
SELECT [Ticker], CONVERT(float,[DataDate])
FROM [FinData].[dbo].[Date_Ticker_Shell$]
WHERE [Date_Ticker_Shell$].[DataDate] between '01/2/2007' AND '10/26/2015'


UPDATE [intradayOHLC_Selected_Closing_Prices_Staging]
SET [Date 250 Days Ago]=CONVERT(float,[Date Calculations].[Date 250 Days Ago])
FROM [Date Calculations]
WHERE [intradayOHLC_Selected_Closing_Prices_Staging].[Date]=[Date Calculations].[Date]

UPDATE [intradayOHLC_Selected_Closing_Prices_Staging]
SET [Close]=[dailyOHLC].[Close]
FROM [dailyOHLC]
WHERE [intradayOHLC_Selected_Closing_Prices_Staging].[Date]=[dailyOHLC].[Date] And [intradayOHLC_Selected_Closing_Prices_Staging].[Ticker]=[dailyOHLC].[Ticker]

CREATE TABLE [dbo].[intradayOHLC_Selected_Closing_Prices] (
[Ticker] varchar(50),
[Date] float,
[Date 250 Days Ago] float,
[Close] float,
[Aggregated 250 Day Closing Prices] float,
[Average 250 Day Price] float
)

INSERT INTO [intradayOHLC_Selected_Closing_Prices] ([Ticker], [Date], [Date 250 Days Ago], [Close], [Aggregated 250 Day Closing Prices])
SELECT [Ticker], [Date], [Date 250 Days Ago], [Close],
SUM(CASE WHEN [Date] between [Date] AND [Date 250 Days Ago] THEN [Close] ELSE 0 END) [Aggregated 250 Day Closing Prices]
FROM [FinData].[dbo].[intradayOHLC_Selected_Closing_Prices_Staging]
GROUP BY [Ticker], [Date], [Date 250 Days Ago], [Close]

2 个答案:

答案 0 :(得分:0)

为什么要将日期转换为数字? SQL Server具有非常好的日期函数和运算符。你应该使用它们。

但是,我认为你的问题是between。假设[Date 250 Days Ago]早于[Date],则您的界限顺序错误。 between的第一个界限需要小于第二个界限。

所以试试:

SELECT [Ticker], [Date], [Date 250 Days Ago], [Close],
       SUM(CASE WHEN [Date] between [Date 250 Days Ago] AND [Date] 
                THEN [Close] ELSE 0 END) as [Aggregated 250 Day Closing Prices]
FROM [FinData].[dbo].[intradayOHLC_Selected_Closing_Prices_Staging]
GROUP BY [Ticker], [Date], [Date 250 Days Ago], [Close];

看到这一点,我认为你只需要[Ticker]中的GROUP BY。所以我猜你真的想要:

SELECT [Ticker],
       SUM(CASE WHEN [Date] between [Date 250 Days Ago] AND [Date] 
                THEN [Close] ELSE 0 END) as [Aggregated 250 Day Closing Prices]
FROM [FinData].[dbo].[intradayOHLC_Selected_Closing_Prices_Staging]
GROUP BY [Ticker]

答案 1 :(得分:-1)

非常感谢,戈登。我按照你的建议清理了代码,但我的总和现在在宇宙中的所有日期聚合,而不是我试图在Staging表中引用的指定日期范围。

要明确的是,我正在寻找每日,滚动1年的平均值,所以我的最终输出需要包括Ticker和Date,这样我每天获得1行,每个自动收报机(如果这是有道理的)。您可以指向我的任何方向都很棒! 感谢

CREATE TABLE [dbo].[intradayOHLC_Selected_Closing_Prices_Staging] (
[Ticker] varchar(50),
[Date] datetime,
[Date 250 Days Ago] datetime,
[Close] float
)

INSERT INTO [intradayOHLC_Selected_Closing_Prices_Staging] ([Ticker], [Date])
SELECT [Ticker], [DataDate]
FROM [FinData].[dbo].[Date_Ticker_Shell$]
WHERE [Date_Ticker_Shell$].[DataDate] between '01/2/2007' AND '10/26/2015'

UPDATE [intradayOHLC_Selected_Closing_Prices_Staging]
SET [Date 250 Days Ago]=[Date Calculations].[Date 250 Days Ago]
FROM [Date Calculations]
WHERE [intradayOHLC_Selected_Closing_Prices_Staging].[Date]=[Date Calculations].[Date]

UPDATE [intradayOHLC_Selected_Closing_Prices_Staging]
SET [Close]=[dailyOHLC].[Close]
FROM [dailyOHLC]
WHERE [intradayOHLC_Selected_Closing_Prices_Staging].[Date]=[dailyOHLC].[Date] And [intradayOHLC_Selected_Closing_Prices_Staging].[Ticker]=[dailyOHLC].[Ticker]


CREATE TABLE [dbo].[intradayOHLC_Selected_Closing_Prices] (
[Ticker] varchar(50),
[Date] datetime,
[Date 250 Days Ago] datetime,
[Close] float,
[Aggregated 250 Day Closing Prices] float,
[Average 250 Day Price] float
)

INSERT INTO [intradayOHLC_Selected_Closing_Prices] ([Ticker],[Aggregated 250 Day Closing Prices])
SELECT [Ticker],
SUM(CASE WHEN [Date] between [Date 250 Days Ago] AND [Date] 
THEN [Close] ELSE 0 END) as [Aggregated 250 Day Closing Prices]
FROM [FinData].[dbo].[intradayOHLC_Selected_Closing_Prices_Staging]
GROUP BY [Ticker]