将数据插入临时表,一个表中的多个列

时间:2013-08-01 19:35:10

标签: sql sql-server-2012

我有一个名为Top40的表,其中包含列:ID(Int),Date(日期)和Price(Decimal)。 ID代表股票。日期格式为2013-04-14。每行都有一个带有日期和价格数据的ID,这意味着每列中有多个相同的ID和日期(我想使用这两列的约束进行选择),因为ID将具有一系列价格。

表格示例

ID      Date        Price
    1       2012/05/02  23.5
    1       2012/05/03  25.2
    1       2012/05/04  22.5
    1       2012/05/05  22.2
    1       2012/05/06  26.5
    2       2012/05/02  143.5
    2       2012/05/03  145.2
    2       2012/05/04  142.2
    2       2012/05/05  146.5
    3       2012/05/02  83.5
    3       2012/05/03  85.2
    3       2012/05/04  80.5

查询示例

我希望能够在Top40的日期范围之间选择日期,ID 1价格和ID 3价格,并将其输出为三列,按日期列排序。更进一步的是将其插入临时表 - 对所需的数据集,选择的范围和股票执行数学计算/公式,如果有更好的方法请评论。

更正结果示例

Date        X           Y
2012-05-02  23.5    83.5    
2012-05-03  25.2    85.2    
2012-05-04  22.5    80.2

任何帮助和建议将不胜感激,

由于


3 个答案:

答案 0 :(得分:1)

请尝试以下操作。

CREATE TABLE #temp (
  Date date,
  x money,
  y money
  )
;

SELECT
 Date,
 MAX(CASE WHEN id=1 THEN price END) AS x,
 MAX(CASE WHEN id=3 THEN price END) AS y
FROM Top40
WHERE Date BETWEEN '2012-05-02' AND '2012-05-04'
GROUP BY 
 Date
;

请参阅SQL Fiddle了解工作示例

修改 要在x和y列上使用LAG窗口函数,您必须首先使用公用表表达式或CTE。

WITH prices AS(
SELECT
 Date as myDate,
 MAX(CASE WHEN id=1 THEN price END) AS x,
 MAX(CASE WHEN id=3 THEN price END) AS y
FROM Top40
WHERE Date BETWEEN '2012-05-02' AND '2012-05-04'
GROUP BY 
 Date
 )
SELECT
 myDate,
 p.x,
 (p.x/(LAG(p.x) OVER (ORDER BY MyDate))-1) as x_return,
 p.y,
 (p.y/(LAG(p.y) OVER (ORDER BY MyDate))-1) as y_return
FROM prices p
ORDER BY
 myDate
;

例如,请参阅新的SQL Fiddle

答案 1 :(得分:0)

在代码中执行此操作的最简单方法(尽管对于大型数据集可能效果不佳)是执行以下操作:

SELECT [Date], x = MAX(CASE WHEN ID = 1 THEN PRICE END)
, y = MAX(CASE WHEN ID = 3 THEN PRICE END)
INTO #tmp
FROM Top40
GROUP BY [Date]

答案 2 :(得分:0)

或者...

select Date , t1.Price as Stock_1_Price , t2.Price as Stock_3_price
from      ( select "Date" , max(Price) as Price from myData where ID = 1 group by "Date" ) t1
full join ( select "Date" , max(Price) as Price from myData where ID = 3 group by "Date" ) t2 on t2.Date = t1.Date

就填充临时表而言,任何常用方法都有效:

  • 表变量:

    declare @work table
    (
      yyyymmdd      varchar(32) not null ,
      stock_1_price money null ,
      stock_3_price money null
    )
    
    insert @work ( yyyymmdd , stock_1_price , stock_3_price )
    select Date , t1.Price as Stock_1_Price , t2.Price as Stock_3_price
    from      ( select "Date" , max(Price) as Price from myData where ID = 1 group by "Date" ) t1
    full join ( select "Date" , max(Price) as Price from myData where ID = 3 group by "Date" ) t2 on t2.Date = t1.Date
    
  • 在tempdb中声明临时表

    create table #work
    (
      yyyymmdd      varchar(32) not null primary key clustered ,
      stock_1_price money null ,
      stock_3_price money null
    )
    
    insert #work ( yyyymmdd , stock_1_price , stock_3_price )
    select Date , t1.Price as Stock_1_Price , t2.Price as Stock_3_price
    from      ( select "Date" , max(Price) as Price from myData where ID = 1 group by "Date" ) t1
    full join ( select "Date" , max(Price) as Price from myData where ID = 3 group by "Date" ) t2 on t2.Date = t1.Date
    
  • 通过select into在tempdb中非声明临时表:

    select Date , t1.Price as Stock_1_Price , t2.Price as Stock_3_price
    into #work
    from      ( select "Date" , max(Price) as Price from myData where ID = 1 group by "Date" ) t1
    full join ( select "Date" , max(Price) as Price from myData where ID = 3 group by "Date" ) t2 on t2.Date = t1.Date