简体   繁体   English

如何提取当前和过去一个月的数据SQL Server

[英]How to pull current and past month data SQL Server

I need to list the trade dates, and stock closing prices for the each month of the year (which I will refer to as the 'current' month) and the stock closing price for the month preceding the 'current' month. 我需要列出交易日期,一年中每个月的股票收盘价(我将其称为“当前”月)和“当前”月前一个月的股票收盘价。

The StockData table I am using has has columns for date, Stock high, low, opening, closing, and volume for each trade date and ticker symbol. 我正在使用的StockData表具有每个交易日期和股票代码的日期,股票高,低,开盘,收盘和交易量列。

Initially, I used AVG : 最初,我使用AVG

SELECT 
    YEAR(TradeDate) AS Year, 
    MONTH(TradeDate) AS Month, 
    TickerSymbol, 
    AVG(ST_Close) AS CurrentClose,
    LAG (AVG(ST_Close), 1, 0) OVER (ORDER BY TickerSymbol, YEAR(TradeDate), MONTH(TradeDate), TickerSymbol) AS PreviousMonthClose,
FROM 
    StockData
WHERE
    TradeDate >= '2000-01-01' AND ST_Close IS NOT NULL
GROUP BY 
    YEAR(TradeDate), MONTH(TradeDate), TickerSymbol
ORDER BY 
    YEAR(TradeDate), MONTH(TradeDate), TickerSymbol;

But instead of using the average, I'd like to pull the stock closing price from the last day of each month, and the previous last day of the last month (for April 2010, I want to show the Stock Closing price on April 30th, 2010 and March 31st, 2010) 但是,我不想使用平均值,而是想将股票的收盘价从每个月的最后一天以及上个月的前一天的最后一天拉下来(对于2010年4月,我想显示4月30日的收盘价,2010年和2010年3月31日)

Any ideas on how to do this? 有关如何执行此操作的任何想法?

You can use window functions and conditional aggregation: 您可以使用窗口函数和条件聚合:

SELECT sd.*
FROM (SELECT sd.*, 
             ROW_NUMBER() OVER (PARTITION BY TickerSymbol, YEAR(TradeDate), MONTH(TradeDate)
                                ORDER BY TradeDate DESC) as seqnum
      FROM StockData sd
      WHERE TradeDate >= '2000-01-01' AND ST_Close IS NOT NULL
     ) sd
WHERE seqnum = 1
ORDER BY TradeDate, TickerSymbol;

This gets all the information from the last record for each month. 这将从每个月的最新记录中获取所有信息。

ORDER BY YEAR(TradeDate), MONTH(TradeDate), TickerSymbol; 按年份排序(交易日期),月份(交易日期),TickerSymbol;

Below is one method, which assumes the previous close value you need is from the date the stock last traded during the previous month. 以下是一种方法,该方法假定您需要的上一个收盘价是从上个月股票最后交易的日期起。

WITH
    daily_close AS (
        SELECT  
            TradeDate
          , TickerSymbol
          , ST_Close
          , ROW_NUMBER() OVER(PARTITION BY TickerSymbol, EOMONTH(TradeDate) ORDER BY TradeDate DESC) AS day_num
        FROM    dbo.StockData
        WHERE   TradeDate >= '2012-01-01'
                AND ST_Close IS NOT NULL
    )
SELECT
        YEAR(curr.TradeDate) AS Year
      , MONTH(curr.TradeDate) AS Month
      , curr.TickerSymbol
      , curr.ST_Close AS CurrentMonthClose
      , prev.ST_Close AS PriorMonthClose
FROM daily_close AS curr
LEFT JOIN daily_close AS prev ON
            prev.TickerSymbol = curr.TickerSymbol
            AND prev.TradeDate < DATEADD(month, -1, DATEADD(day, 1, curr.TradeDate))
            AND prev.TradeDate >= DATEADD(month, -2, DATEADD(day, 1, curr.TradeDate))
            AND prev.day_num = 1
WHERE 
    curr.day_num = 1
ORDER BY
      Year
    , Month
    , TickerSymbol;

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

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