2012-03-01 85 views
0

我試圖選擇一次行,有(一些指定)與上一行相同的值。比較行與上一行

我可以選擇我想要像下面這樣:

SELECT dpr_ts 
     , dpr_open 
     , dpr_volume 
     , LAG(dpr_open, 1, 0) over(ORDER BY dpr_ts) AS po 
     , LAG(dpr_close, 1, 0) over(ORDER BY dpr_ts) AS pc 
     , LAG(dpr_volume, 1, 0) over(ORDER BY dpr_ts) AS pv 
    FROM dpr 
ORDER BY dpr_ts; 

如何,可以指定在where子句中,只有重複應該出現?

我的意思是我想是這樣(這doent工作,但把它只讓你一個想法是什麼我想要):

SELECT dpr_ts 
     , dpr_open 
     , dpr_volume 
     , LAG(dpr_open, 1, 0) over(ORDER BY dpr_ts) AS po 
where po = dpr_volume; 

感謝 問候

回答

3

我想你想要什麼是:

select * 
    from (
    select dpr_ts 
     , dpr_open 
     , dpr_volume 
     , LAG(dpr_open, 1, 0) over(ORDER BY dpr_ts) AS po 
     from dpr 
    order by drp_ts 
    ) x 
where x.po = x.dpr_volume