2012-03-01 95 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