왕초보 문의드립니다.
아래 표와 같이 특정장비가 상태를 보고하고 서버는 이신호를 받아 DB에 저장합니다.
상태보고는 이벤트발생시와 고정주기보고시 진행됩니다.
이벤트보고가 온 시간만 따로 뽑아 테이블을 만들고 싶습니다.
그런데 문제는 이벤트 발생시와 고정주기보고시를 DB에서는 구분할수 있는 컬럼이 없습니다.
그래서 고민해보니
최초 보고(일일단위)는 상태값(st)이 0입니다. -- 상태값(st)이 0인 최초 보고 시간 확보
이후 보고되는 0값은 이전상태와 동일하므로 모두 고정주기보고이므로 무시하다가,
상태값(st)이 1로 변경되는 시점의 시간 확보,
이후 상태값이 동일하게 올라온 부분은 버리고 이전상태와 다른 0으로 변경되는 시점의 시간 확보...
하루단위로 구분되어 있습니다.
이런식으로 하면 구분할 수 있을 것같습니다.
이게 쿼리로 가능할까요?
고수님들의 답변부탁드립니다.
equip_no | rec_t | st | |
eq101 | 12:00:00 | 0 | 1 |
eq102 | 12:00:07 | 0 | 2 |
eq103 | 12:00:18 | 0 | 3 |
eq104 | 12:00:26 | 0 | 4 |
eq101 | 12:00:34 | 0 | 5 |
eq102 | 12:00:42 | 0 | 6 |
eq103 | 12:00:50 | 0 | 7 |
eq104 | 12:00:58 | 0 | 8 |
eq101 | 12:01:06 | 0 | 9 |
eq102 | 12:01:14 | 0 | 10 |
eq103 | 12:01:22 | 0 | 11 |
eq104 | 12:01:30 | 0 | 12 |
eq101 | 12:01:38 | 1 | 13 |
eq102 | 12:01:46 | 0 | 14 |
eq103 | 12:01:54 | 0 | 15 |
eq104 | 12:02:02 | 1 | 16 |
eq101 | 12:02:10 | 1 | 17 |
eq102 | 12:02:18 | 0 | 18 |
eq103 | 12:02:26 | 1 | 19 |
eq104 | 12:02:34 | 1 | 20 |
eq101 | 12:02:42 | 0 | 21 |
eq102 | 12:02:50 | 1 | 22 |
eq103 | 12:02:58 | 1 | 23 |
eq104 | 12:03:06 | 1 | 24 |
eq101 | 12:03:14 | 0 | 25 |
eq102 | 12:03:22 | 1 | 26 |
eq103 | 12:03:30 | 1 | 27 |
eq104 | 12:03:38 | 1 | 28 |
eq101 | 12:03:46 | 1 | 29 |
eq102 | 12:03:54 | 0 | 30 |
eq103 | 12:04:02 | 0 | 31 |
eq104 | 12:04:10 | 1 | 32 |
eq101 | 12:04:18 | 1 | 33 |
eq102 | 12:04:26 | 0 | 34 |
eq103 | 12:04:34 | 0 | 35 |
eq104 | 12:04:42 | 1 | 36 |
eq101 | 12:04:50 | 0 | 37 |
eq102 | 12:04:58 | 0 | 38 |
eq103 | 12:05:06 | 1 | 39 |
eq104 | 12:05:14 | 0 | 40 |
Comment 1
-
건우아빠
2014.02.27 00:01
with temp as (
select 'eq101' equip_no,'12:00:00' rec_t, 0 st ,1 no union all
select 'eq102' ,'12:00:07', 0 ,2 union all
select 'eq103' ,'12:00:18', 0 ,3 union all
select 'eq104' ,'12:00:26', 0 ,4 union all
select 'eq101' ,'12:00:34', 0 ,5 union all
select 'eq102' ,'12:00:42', 0 ,6 union all
select 'eq103' ,'12:00:50', 0 ,7 union all
select 'eq104' ,'12:00:58', 0 ,8 union all
select 'eq101' ,'12:01:06', 0 ,9 union all
select 'eq102' ,'12:01:14', 0 ,10 union all
select 'eq103' ,'12:01:22', 0 ,11 union all
select 'eq104' ,'12:01:30', 0 ,12 union all
select 'eq101' ,'12:01:38', 1 ,13 union all
select 'eq102' ,'12:01:46', 0 ,14 union all
select 'eq103' ,'12:01:54', 0 ,15 union all
select 'eq104' ,'12:02:02', 1 ,16 union all
select 'eq101' ,'12:02:10', 1 ,17 union all
select 'eq102' ,'12:02:18', 0 ,18 union all
select 'eq103' ,'12:02:26', 1 ,19 union all
select 'eq104' ,'12:02:34', 1 ,20 union all
select 'eq101' ,'12:02:42', 0 ,21 union all
select 'eq102' ,'12:02:50', 1 ,22 union all
select 'eq103' ,'12:02:58', 1 ,23 union all
select 'eq104' ,'12:03:06', 1 ,24 union all
select 'eq101' ,'12:03:14', 0 ,25 union all
select 'eq102' ,'12:03:22', 1 ,26 union all
select 'eq103' ,'12:03:30', 1 ,27 union all
select 'eq104' ,'12:03:38', 1 ,28 union all
select 'eq101' ,'12:03:46', 1 ,29 union all
select 'eq102' ,'12:03:54', 0 ,30 union all
select 'eq103' ,'12:04:02', 0 ,31 union all
select 'eq104' ,'12:04:10', 1 ,32 union all
select 'eq101' ,'12:04:18', 1 ,33 union all
select 'eq102' ,'12:04:26', 0 ,34 union all
select 'eq103' ,'12:04:34', 0 ,35 union all
select 'eq104' ,'12:04:42', 1 ,36 union all
select 'eq101' ,'12:04:50', 0 ,37 union all
select 'eq102' ,'12:04:58', 0 ,38 union all
select 'eq103' ,'12:05:06', 1 ,39 union all
select 'eq104' ,'12:05:14', 0 ,40 ),
res as (
select * , ROW_NUMBER() over (partition by a.equip_no order by a.rec_t ) n_no
from temp a )
select a.equip_no ,a.rec_t ,a.st , a.no , case when a.st <> isnull(b.st,99) then 1 else 0 end chk
from res a left join res b on a.equip_no = b.equip_no and a.n_no = b.n_no + 1
order by a.no
chk값이 1인게 위 질문에서 색깔 칠한 부분이 될거구요...
그중에서 st 가 0인게 빨간색 1인게 파란색이겠네요...