SQL内部联接无法从上一个日期获得更改

时间:2012-12-18 09:35:21

标签: sql sql-server sql-server-2008

我有一张包含以下数据的表格:

TradeDate   Stock   BuySell   DayClose
--------------------------------------
10-Dec-12   ABC        1         11
10-Dec-12   ABC        2         12
11-Dec-12   ABC        1         11.5
11-Dec-12   ABC        2         12.5
11-Dec-12   DEF        1         15
11-Dec-12   DEF        2         16

我希望在2012年12月11日的特定日期查询它以获得以下输出:

Stock   Buy     Sell    Mid     Change
--------------------------------------
 ABC    11.5    12.5    12.0    0.5
 DEF    15      16      15.5    

由于DEF没有上一个日期的数据,因此更改应为空白。

我创建了以下查询:

Select Stock, 
AVG(CASE BuySell WHEN 1 THEN DayClose END) AS 'Buy', 
AVG(CASE BuySell WHEN 2 THEN DayClose END) As 'Sell', 
Sum(DayClose/2) as 'Mid', 
Sum(Change/2) AS Change

FROM (
select t1.Stock, t1.BuySell, t1.DayClose, Sum(t1.DayClose - t2.DayClose) as Change 

FROM #myTable as t1 inner join #myTable as t2 on 
t1.Stock = t2.Stock

where 
t1.TradeDate = '2012-12-11'  AND 
t2.TradeDate = (SELECT TOP 1 TradeDate FROM #myTable WHERE TradeDate < '2012-12-11' ORDER BY TradeDate DESC)  

GROUP BY 
t1.Stock, t1.buysell, t1.dayclose ) AS P1 GROUP BY stock

我为此创建了一个临时表#mytable:

drop table #mytable
CREATE TABLE #myTable 
(
    TradeDate datetime,
    stock varchar(20),
    buysell int, 
    dayclose  decimal(10,2)
)
insert into #mytable values ('10-dec-2012', 'abc' , 1, 11)
insert into #mytable values ('10-dec-2012', 'abc' , 2, 12)
insert into #mytable values ('11-dec-2012', 'abc' , 1, 11.5)
insert into #mytable values ('11-dec-2012', 'abc' , 2, 12.5)
insert into #mytable values ('11-dec-2012', 'def' , 1, 15)
insert into #mytable values ('11-dec-2012', 'def' , 2, 16)

但我无法获得所需的输出,而是获得

Stock         Buy         Sell      Mid     Change
--------------------------------------------------------------
abc           11.500000 12.500000   12.00000    1.00    

有人能告诉我哪里出错了。我好像迷失在这里。

谢谢, 莫妮卡

3 个答案:

答案 0 :(得分:1)

Select Stock, 
AVG(CASE BuySell WHEN 1 THEN DayClose END) AS 'Buy', 
AVG(CASE BuySell WHEN 2 THEN DayClose END) As 'Sell', 
Sum(DayClose/2) as 'Mid', 
Sum(Change/2) AS Change

FROM (
select t1.Stock, t1.BuySell, t1.DayClose, Sum( t1.DayClose - t2.DayClose ) as Change 
FROM #myTable as t1 left join #myTable as t2 on t2.TradeDate = (SELECT TOP 1 TradeDate FROM #myTable WHERE TradeDate < t1.TradeDate ORDER BY TradeDate DESC)   
and t1.Stock = t2.Stock and t1.buysell=t2.buysell 

where 
t1.TradeDate = '11-12-2012'  

答案 1 :(得分:1)

试试这个:

SELECT   a.TradeDate
        ,a.stock
        ,SUM(CASE WHEN a.BuySell = 1 THEN a.DayClose ELSE 0 END) Buy
        ,SUM(CASE WHEN a.BuySell = 2 THEN a.DayClose ELSE 0 END) Sell
        ,SUM(a.DayClose) / 2 AS Mid
INTO    #temp
FROM    #mytable a
GROUP   BY a.TradeDate, a.stock


SELECT  t.*,
        t.Mid - previousRecord.Mid AS Change
FROM    #temp t
LEFT JOIN   
        #temp previousRecord ON 
        previousRecord.TradeDate = DATEADD(DAY, -1, t.TradeDate)
AND     previousRecord.stock = t.stock

DROP TABLE #temp

您现在要做的就是选择日期数据。

答案 2 :(得分:1)

请尝试:

;WITH T1 as(
SELECT   a.TradeDate
        ,a.stock
        ,SUM(CASE WHEN a.BuySell = 1 THEN a.DayClose ELSE 0 END) Buy
        ,SUM(CASE WHEN a.BuySell = 2 THEN a.DayClose ELSE 0 END) Sell
        ,SUM(a.DayClose) / 2 AS Mid
FROM    #mytable a
GROUP   BY a.TradeDate, a.stock
)SELECT  t.*,
        t.Mid - PR.Mid AS Change
FROM    T1 t
LEFT JOIN   
        T1 PR ON 
        PR.TradeDate = DATEADD(DAY, -1, t.TradeDate)
AND     PR.stock = t.stock