你沒有指定你的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 |
Here's the SQL Fiddle如果您想玩弄或調整另一個RDBMS的語法。
什麼是你的關係數據庫管理系統,你到目前爲止嘗試過了什麼? –
此外,請告訴我們[您嘗試過的](http://www.whathaveyoutried.com) – Kermit
爲什麼每天都有新的行?爲什麼不在創建新記錄時改變值?如果「當日」記錄丟失會發生什麼?或者如果有差距?如果它回到'三天前'的價值會發生什麼? –