HandKot |
Добрый всем день
есть такая задача имеем таблицу
with t as (
select 1 id, 0 ves, sysdate dd from dual union all
select 1 id, 10 ves, sysdate+2 dd from dual union all
select 2 id, 0 ves, sysdate+3 dd from dual union all
select 2 id, 0 ves, sysdate+4 dd from dual union all
select 1 id, 10 ves, sysdate+5 dd from dual union all
select 1 id, 0 ves, sysdate+6 dd from dual union all
select 2 id, 10 ves, sysdate+7 dd from dual union all
select 2 id, 10 ves, sysdate+8 dd from dual union all
select 2 id, 0 ves, sysdate+9 dd from dual union all
select 2 id, 0 ves, sysdate+10 dd from dual union all
select 1 id, 0 ves, sysdate+11 dd from dual union all
select 1 id, 10 ves, sysdate+12 dd from dual union all
select 2 id, 0 ves, sysdate+13 dd from dual
) необходимо найти записи, для которых верно условие: не менее двух подряд 0 в поле ves с группировкой по id
т.е для данного набора получить
id | ves | dd | 1 | 0 | 15.12.2007 15:48:56 | 1 | 0 | 10.12.2007 15:48:56 | 2 | 0 | 14.12.2007 15:48:56 | 2 | 0 | 13.12.2007 15:48:56 | 2 | 0 | 17.12.2007 15:48:56 | 2 | 0 | 08.12.2007 15:48:56 | 2 | 0 | 07.12.2007 15:48:56 |
|
Спасибо
I Have Nine Lives You Have One Only THINK! |
не претендую на оригинальность и оптимальность(Oracle 8.1.7)
select k.id, k.ves, k.dd from(select w.*, LEAD(w.ves, 1) OVER (ORDER BY w.id, w.dd) as ves_next,
LAG(w.ves, 1) OVER (ORDER BY w.id, w.dd) AS ves_prev from(
select 1 id, 0 ves, sysdate dd from dual union all
select 1 id, 10 ves, sysdate+2 dd from dual union all
select 2 id, 0 ves, sysdate+3 dd from dual union all
select 2 id, 0 ves, sysdate+4 dd from dual union all
select 1 id, 10 ves, sysdate+5 dd from dual union all
select 1 id, 0 ves, sysdate+6 dd from dual union all
select 2 id, 10 ves, sysdate+7 dd from dual union all
select 2 id, 10 ves, sysdate+8 dd from dual union all
select 2 id, 0 ves, sysdate+9 dd from dual union all
select 2 id, 0 ves, sysdate+10 dd from dual union all
select 1 id, 0 ves, sysdate+11 dd from dual union all
select 1 id, 10 ves, sysdate+12 dd from dual union all
select 2 id, 0 ves, sysdate+13 dd from dual
order by 1,3
)w)k
where k.ves=0 and (k.ves_next=0 or k.ves_prev = 0)