SQL Server 2012天数下降

时间:2018-04-14 16:18:05

标签: sql-server-2012 days stocks

我有一张股票价格表:

DECLARE @table TABLE (ClosingDate DATE, Ticker VarChar(6), Price Decimal (6,2))

INSERT INTO @Table
VALUES ('1/1/13' , 'ABC' , '100.00')
          ,('1/2/13' , 'ABC' , '101.50')  
          ,('1/3/13' , 'ABC' , '99.80')
          ,('1/4/13' , 'ABC' , '95.50')
          ,('1/5/13' , 'ABC' , '78.00')
          ,('1/1/13' , 'JKL' , '34.57')
          ,('1/2/13' , 'JKL' , '33.99')  
          ,('1/3/13' , 'JKL' , '31.85')
          ,('1/4/13' , 'JKL' , '30.11')
          ,('1/5/13' , 'JKL' , '45.00')
          ,('1/1/13' , 'XYZ' , '11.50')
          ,('1/2/13' , 'XYZ' , '12.10')  
          ,('1/3/13' , 'XYZ' , '17.15')
          ,('1/4/13' , 'XYZ' , '14.10')
          ,('1/5/13' , 'XYZ' , '15.55')

我计算每个股票代码的下跌(来自最高价格或最高价格的百分比):

SELECT Ticker,
    t.ClosingDate,
    t.Price, 
    MAX(t.[Price]) OVER (PARTITION BY Ticker ORDER BY ClosingDate) AS max_price,
    (t.[Price] / MAX(t.[Price]) OVER (PARTITION BY Ticker ORDER BY ClosingDate)) - 1 AS Drawdown
FROM 
    @Table t;

输出:

  Ticker    ClosingDate Price   max_price   Drawdown
  -----------------------------------------------------
    ABC     2013-01-01  100.00  100.00     0.000000000
    ABC     2013-01-02  101.50  101.50     0.000000000
    ABC     2013-01-03  99.80   101.50    -0.016748769
    ABC     2013-01-04  95.50   101.50    -0.059113301
    ABC     2013-01-05  78.00   101.50    -0.231527094
    JKL     2013-01-01  34.57   34.57      0.000000000
    JKL     2013-01-02  33.99   34.57     -0.016777553
    JKL     2013-01-03  31.85   34.57     -0.078680938
    JKL     2013-01-04  30.11   34.57     -0.129013596
    JKL     2013-01-05  45.00   45.00      0.000000000
    XYZ     2013-01-01  11.50   11.50      0.000000000
    XYZ     2013-01-02  12.10   12.10      0.000000000
    XYZ     2013-01-03  17.15   17.15      0.000000000
    XYZ     2013-01-04  14.10   17.15     -0.177842566
    XYZ     2013-01-05  15.55   17.15     -0.093294461

新的高价被指定为缩编或0。

如何在缩编中添加天数?

drawdown = 0的任何日期将天数计数器重置为0并且每天构建时仍为缩编(价格<最高价格)

这是我的预期输出:

Ticker  ClosingDate  Price       max_price  Drawdown    Days in DD
--------------------------------------------------------------------
ABC     1/1/2013     100.00      100.00      0.0000     0
ABC     1/2/2013     101.50      101.50      0.0000     0
ABC     1/3/2013      99.80      101.50     -0.0167     1
ABC     1/4/2013      95.50      101.50     -0.0591     2
ABC     1/5/2013      78.00      101.50     -0.2315     3
JKL     1/1/2013      34.57       34.57      0.0000     0
JKL     1/2/2013      33.99       34.57     -0.0168     1
JKL     1/3/2013      31.85       34.57     -0.0787     2
JKL     1/4/2013      30.11       34.57     -0.1290     3
JKL     1/5/2013      45.00       45.00      0.0000     0
XYZ     1/1/2013      11.50       11.50      0.0000     0
XYZ     1/2/2013      12.10       12.10      0.0000     0
XYZ     1/3/2013      17.15       17.15      0.0000     0
XYZ     1/4/2013      14.10       17.15     -0.1778     1
XYZ     1/5/2013      15.55       17.15     -0.0933     2

0 个答案:

没有答案