0

我有一个包含列的表:ID(Int)、Date(Date)和Price(Decimal)。Date列格式为 2013-04-14:

表格示例

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

查询示例:

我希望能够在表中的日期范围之间选择所有ID1 和3 的数据,并将其放在一个包含三列的表中,按列排序。此外,我想将其插入到临时表中以对数据执行数学计算。如果有更好的方法,请发表评论。IDDate

正确结果示例

Date        ID1     ID3
2012-05-02  23.5    83.5    
2012-05-03  25.2    85.2    
2012-05-04  22.5    80.2

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

谢谢


4

3 回答 3

2

试试下面的。

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

于 2013-08-02T19:34:15.877 回答
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]
于 2013-08-02T19:19:28.163 回答
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
    
  • 通过以下方式在 tempdb 中未声明临时表select into

    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
    
于 2013-08-02T19:47:54.730 回答