如何找到在连续日期的具体值

问题描述 投票:1回答:1

我需要你的一点点问题的帮助。

我使用的MS Access与数据库工作,我需要解决一个查询。我的查询问:找到CUSTOMER_ID和TRANSC_ID其中200和500之间的连续2个值内相同transc_id。

我解释。

我在这个格式的表格:

CUSTOMER_ID    TRANSC_ID    VALUE   VALUE_DATE
51             10           15      29-12-1999
51             10           20      15-07-2000
51             10           35      18-08-2000
51             10           250     30-08-2000
51             10           13      10-09-2000
51             10           450     15-09-2000
51             11           5       15-09-2000
51             11           23      30-09-2000
51             11           490     10-10-2000
51             11           300     12-10-2000
51             11           85      30-10-2000
51             11           98      01-01-2000
53             10           65      15-10-2000
53             10           14      29-12-2000

而我只需要

51             11           490     10-10-2000
51             11           300     12-10-2000

因为这两个值是连续的(和两者是> 250 <500)。

我怎样才能让在MS Access查询获得这个结果呢?

谢谢。

sql ms-access
1个回答
3
投票

您可以使用相关子查询得到的“下一个”和“上”的价值观,然后做比较:

select t.*
from t
where t.value between 200 and 500 and
      ( (select top 1 t2.value
         from t as t2
         where t2.CUSTOMER_ID = t.CUSTOMER_ID and t2.TRANSC_ID = t.TRANSC_ID and
               t2.value_date > t.value_date
         order by t2.value_date
        ) between 200 and 500 or
        (select top 1 t2.value
         from t as t2
         where t2.CUSTOMER_ID = t.CUSTOMER_ID and t2.TRANSC_ID = t.TRANSC_ID and
               t2.value_date < t.value_date
         order by t2.value_date desc
        ) between 200 and 500
       );
© www.soinside.com 2019 - 2024. All rights reserved.