如何 select 每组的前两行,并计算它们在一列中的差异?

How to select the first two row of each group and count difference between them in one column?

我有 table 那样

ID_WE   ID_B    ID_WO   RDATA   RSIZE
11111   22      1   1998-10-01  14
11111   22      2   1998-09-30  17
11111   23      1   1998-10-01  23
11112   22      1   1998-09-30  14

ID_WEID_BID_WO一起是主键。对于每个组合 id-we,id_b 很少 ID_WO。每个ID_WO都有很多reads,关于reads的信息在RDATARSIZE

我需要像那样获取 table

ID_WE   ID_B    ID_WO   DAYS    DIF
11111   22      1       1       0

DIF 是给定 ID_WO 的最后两次读取之间 RSIZE 的差异, DAYS 是最后两次读取之间经过了多少天

它可能需要一些分组依据,也许需要 max(rdata) 来计算天数和大小的差异。我真的不知道如何获得那样的结果。对于如何获得所需结果的任何提示,我将不胜感激。

试试这个

SELECT MIN(ID_WE) ID_WE,ID_B,ID_WO,DAYS,DIF
FROM
(
     SELECT MIN(ID_WE) ID_WE,MIN(ID_B) ID_B,MIN(ID_WO) ID_WO, 
            MAX(RDATA)-Min(RDATA) DAYS,MAX(RSIZE)-Min(RSIZE) DIF   
     FROM TABLE1
     GROUP BY ID_WE,ID_B
)AS G
GROUP BY ID_WE

我构建了一些获取最后两个元素的东西,而不是 TABLE1:

SELECT MIN(ID_WE) ID_WE,ID_B,ID_WO,DAYS,DIF
FROM
 (
 SELECT MIN(d.ID_WE) ID_WE,MIN(d.ID_B) ID_B,MIN(d.ID_WO) ID_WO, 
        MAX(d.rdata)-Min(d.rdata) DAYS,MAX(d.RSIZE)-Min(d.RSIZE) DIF   
 FROM 
     (  select t.id_we, t.id_b, t.id_wo, t.max from 
       (( select b.id_we, b.id_b, b.id_wo, max(b.rdata) as max
            from test b
            where b.rdata NOT IN
                (select max(a.rdata)
                from test a
                group by a.id_we, a.id_b, a.id_wo
                )
            group by b.id_we, b.id_b, b.id_wo
        ) union
        (
          select e.id_we, e.id_b, e.id_wo, max(e.rdata)
          from test e
          group by e.id_we, e.id_b, e.id_wo
        )  
       ) t

        ) t4
        join test d on t4.id_we = d.id_we and d.id_b = t4.id_b and d.id_wo = t4.id_wo and d.rdata = t4.max
   GROUP BY t4.ID_WE,t4.ID_B
)AS G
GROUP BY ID_WE;

感谢大家的评论和回答,让我走上了正确的思路。我终于弄明白了。在 Baron Schwartz 的博客 this page 的帮助下(如果有人需要 select SQL 中每个组的 first/least/max 行,它是有用的资源)。

我得到最大的和第二大的 RDATA 并在这样的查询中添加对应的数据 RSIZE,最大的(FOO):

select oo.id_wej,oo.id_ob,oo.id_wo, oo.odata,oo.od
from (select id_wej,id_ob,id_wo,odata from odczyty
where odata = (select max(odata) from odczyty o where o.id_wej=odczyty.id_wej and o.id_ob=odczyty.id_ob and o.id_wo=odczyty.id_wo)) as x
inner join odczyty oo on oo.id_wej=x.id_wej and oo.id_ob=x.id_ob and oo.id_wo=x.id_wo and oo.odata=x.odata
order by id_wej,id_ob,id_wo)as ok
on od.id_wej=ok.id_wej and od.id_ob=ok.id_ob and od.id_wo=ok.id_wo

第二大(BAR):

select o.id_we,o.id_b,o.id_wo, o.rdata,o.rsize
from (select id_we,id_b,id_wo,rdata from odczyty
where rdata =(select max(rdata)
from odczyty o2
where o2.id_we=odczyty.id_we and o2.id_b=odczyty.id_b
and o2.id_wo=odczyty.id_wo and
rdata <(select max(rdata) from odczyty o3
where o3.id_we=o2.id_we and
o3.id_b=o2.id_b and o3.id_wo=o2.id_wo))) as x
inner join odczyty o on o.id_we=x.id_we and o.id_b=x.id_b and o.id_wo=x.id_wo and o.rdata=x.rdata

使用这两个查询我做了这个:

   select od.id_wej,od.id_ob,od.id_wo, ok.odata-od.odata as days,ok.od-od.od as dif
from(BAR)as od  inner join
(FOO)as ok
on od.id_wej=ok.id_wej and od.id_ob=ok.id_ob and od.id_wo=ok.id_wo
order by id_wej, id_ob, id_wo

为了便于阅读,我用 FOO 和 BAR 替换了插入的查询。