RDBMS を指定しませんが、このソリューションはおそらくほとんどのものに置き換えることができます。私は、非null
測定値が毎日存在すると仮定しています。
PostgreSQL 9.1 スキーマのセットアップ:
create table foo(id text, changed_on date, val integer);
insert into foo(id, changed_on, val)
values ('B99', to_date('2012-11-28','yyyy-mm-dd'), 400),
('B99', to_date('2012-11-27','yyyy-mm-dd'), 120),
('B99', to_date('2012-11-26','yyyy-mm-dd'), 120),
('B99', to_date('2012-11-25','yyyy-mm-dd'), 300),
('A12', to_date('2012-11-28','yyyy-mm-dd'), 800),
('A12', to_date('2012-11-27','yyyy-mm-dd'), 800),
('A12', to_date('2012-11-26','yyyy-mm-dd'), 800),
('A12', to_date('2012-11-25','yyyy-mm-dd'), 800),
('B99', to_date('2012-11-28','yyyy-mm-dd'), 100),
('B99', to_date('2012-11-27','yyyy-mm-dd'), 260),
('B99', to_date('2012-11-26','yyyy-mm-dd'), 230),
('B99', to_date('2012-11-25','yyyy-mm-dd'), 230)
;
クエリ:
select id
from foo
where changed_on>=to_date('2012-11-29','yyyy-mm-dd')-3
group by id
having count(distinct val)=1
結果:
| ID |
-------
| A12 |
別の RDBMS の構文を試したり、調整したりしたい場合は、SQL Fiddleを使用してください。