这组计算是否需要动态SQL?

时间:2016-11-23 18:27:07

标签: sql sql-server min

我在SQL Server Manager 2012中处理以下查询时遇到问题。我已经能够迭代地进行计算,但是它使用了太多的处理器时间,因此我需要将它包装到单个查询中。谢谢

    CREATE TABLE [Execution Timing4]
    ([Instance] DATETIME, [Date] DATETIME, [Time] TIME, 
    [Ticker] CHAR (20), 
    [Total_Score] REAL, rank REAL, row_number REAL, 
    [Intraday Return] float, 
    [Prior Close] float, [Closing Print] float, 
    [Close to Close Return] float, [Return Differential] float, 
    [Execute Sale] float, [First Sale] time, [Second Sale] time, 
    [Third Sale] time, [Fourth Sale] time, 
    [Number of Sales] float, [Sale Weighted Return] float);

INSERT INTO [Execution Timing4] ([Instance]
      ,[Date]
      ,[Time]
      ,[Ticker]
      ,[Total_Score]
      ,[rank]
      ,[row_number]
      ,[Intraday Return]
      ,[Prior Close]
      ,[Closing Print]
      ,[Close to Close Return]
      ,[Return Differential]
      ,[Execute Sale]
      ,[First Sale]
      ,[Second Sale]
      ,[Third Sale]
      ,[Fourth Sale])
      SELECT [Instance]
      ,[Date]
      ,[Time]
      ,[Ticker]
      ,[Total_Score]
      ,[rank]
      ,[row_number]
      ,[Intraday Return]
      ,[Prior Close]
      ,[Closing Print]
      ,[Close to Close Return]
      ,[Return Differential]
      ,[Execute Sale]
      ,(SELECT MIN(T.[Time]) FROM Scorecard AS T
      WHERE T.[Ticker] = O2.[Ticker] AND T.[Date] = O2.[Date] 
    AND T.[Execute Sale] = '1')
      FROM [Scorecard] AS O2
      ,(SELECT MIN(T.[Time]) FROM Scorecard AS T
      WHERE T.[Ticker] = O3.[Ticker] AND T.[Date] = O3.[Date] 
    AND T.[Execute Sale] = '1' AND T.[Time] > O3.[First Sale] AND T.[Time] > '09:45:00.0000000')
      FROM [Scorecard] AS O3
      ,(SELECT MIN(T.[Time]) FROM Scorecard AS T
      WHERE T.[Ticker] = O4.[Ticker] AND T.[Date] = O4.[Date] 
    AND T.[Execute Sale] = '1' AND T.[Time] > O4.[Second Sale] AND T.[Time] > '11:40:00.0000000')
      FROM [Scorecard] AS O4
      , (SELECT MIN(T.[Time]) FROM Scorecard AS T
      WHERE T.[Ticker] = O5.[Ticker] AND T.[Date] = O5.[Date] 
    AND T.[Execute Sale] = '1' AND T.[Time] > O5.[Third Sale] AND T.[Time] > '13:50:00.0000000')
      FROM [Scorecard] AS O5

0 个答案:

没有答案