查询匹配第一个更高的匹配并忽略后续更高的值

时间:2014-04-12 11:44:48

标签: sql ms-access-2003

table1的内容:

name  abase1        dz      pline    
 acc       1         1      123.45  
 acc       2         1      128.20
 acc       3         2      130.20
 acc       4         2      135.20

表2

 name          bbase1           chigh
   acc           1              128.20
   acc           2              130.25
   acc           3              145.20 
   acc           4              128.05
   acc           5              148.20   
   acc           6              149.50 
   acc           7              148.70             

我想加入上述两个表格,条件是bbase是> abase and first chigh> pline并忽略特定abase1的后续更大的chigh,并希望得到以下结果

  name      abase1           pline      bbase1      chigh
   acc           1            123.45       3        145.20
   acc           2            128.20       3        145.20  
   acc           3            130.20       5        148.20 
   acc           4            135.20       6        149.50   

您可以在查询或vba中帮助我,因为我的查询无法正常工作

select a.acc,a.abase1,a.pline,b.bbase1,b.chigh from table1 as a  inner join table2 as b on a.name=b.name and a.abase1<b.bbase1 and a.pline <b.chigh

由于

1 个答案:

答案 0 :(得分:0)

试试这个:

select a.acc,a.abase1,a.pline,b.bbase1,b.chigh 
from table1 as a  
inner join table2 as b 
    on a.name=b.name and a.abase1<b.bbase1 and a.pline <b.chigh
left join table2 as c
    on c.name = a.name and a.abase1 < c.bbase1 and a.pline < c.chigh and b.bbase1 > c.bbase1 and b.chigh > c.chigh
where c.bbase1 is null