database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
ㆍOracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
Oracle Q&A 40715 게시물 읽기
No. 40715
쿼리 문의드립니다.
작성자
초초초
작성일
2015-02-10 11:19
조회수
7,465

 sql문의 드립니다.

장비테이블

 

a

b

c

 

데이타테이블

a   에러   201501011100

a   에러   201501011000

a   정상   201501009000

b   정상   201501011100

b   정상   201501011000

이렇게 2가지 테이블에 데이타가 들어있는대요.

원하는 결과는

 

 

a  에러  1시간

b  정상

c  에러 

처럼 뽑아내려고 합니가.

에러가 연속일경우 시간을 구해야되는대..몇개가 연속으로 나올지 고정이 아니라서여..어렵네요

고수님들 도와주세요 ㅠㅠ

이 글에 대한 댓글이 총 2건 있습니다.

-- 아래의 쿼리 참조하세요.

with v_data1 as (
  select 'a' as key1 from dual
  union all select 'b' as key1 from dual
  union all select 'c' as key1 from dual
), v_data2 as (
  select 'a' as key1,'에러' as key2,'201501011100' as key3 from dual
  union all select 'a' as key1,'에러' as key2,'201501011000' as key3 from dual
  union all select 'a' as key1,'정상' as key2,'201501010900' as key3 from dual
  union all select 'a' as key1,'에러' as key2,'201501010800' as key3 from dual
  union all select 'b' as key1,'정상' as key2,'201501011100' as key3 from dual
  union all select 'b' as key1,'정상' as key2,'201501011000' as key3 from dual
  union all select 'b' as key1,'에러' as key2,'201501010900' as key3 from dual
), v_data3 as (
  select a.key1,a.key3 as max_key3 ---> 정상인것의 최신시간을 구한다.
  from   v_data2 a
  where  a.key2 = '정상'
  and    not exists (
    select 1 from v_data2 e1
    where a.key1 = e1.key1
    and   a.key2 = e1.key2
    and   a.key3 < e1.key3
  )
  union
  select a.key1,'201401010000' as max_key3 --> 정상이 없는경우 기준시간을 임의로 셋팅함
  from   v_data1 a
  where  not exists (
    select 1 from v_data2 e1
    where a.key1 = e1.key1
    and   e1.key2 = '정상'
  )
), v_data4 as (
  select a.key1,b.key2,b.key3 --> key1별로 최신 데이타를 가져온다.
  from   v_data1 a
         ,v_data2 b
  where  a.key1 = b.key1
  and    not exists (
    select 1 from v_data2 e1 where b.key1 = e1.key1 and b.key3 < e1.key3
  )
), v_data5 as (
  select  --> 에러구간에 대한 시간구하기
    m.key1
    ,to_char(floor((sysdate - to_date(min(p.key3),'yyyymmddhh24mi'))*24 )) || ' 시간' as hour_txt --> 에러일경우 현재시간부터 에러min시간까지 구하였음. 분단위는 버림
    -- ,to_char(floor((to_date(max(p.key3),'yyyymmddhh24mi') - to_date(min(p.key3),'yyyymmddhh24mi'))*24 )) || ' 시간' as hour_txt --> 에러Max부터 에러min까지의 시간
  from v_data4 m
       ,v_data3 n
       ,v_data2 p
  where m.key1 = n.key1
  and   m.key1 = p.key1
  and   m.key2 = '에러'
  and   p.key3 <= m.key3
  and   p.key3 > n.max_key3
  group by m.key1
)
select
  *
from (
  select --> 데이타가 없는경우 데이타생성
    a.key1,'에러' as key2,'' as hour_txt
  from v_Data1 a
  where not exists (
    select 1 from v_data2 where a.key1 = b.key1
  )
    union all
  select
    a.key1,a.key2,b.hour_txt
  from v_Data4 a
      ,v_data5 b
  where a.key1 = b.key1(+)
)
order by key1

이정재(ljjang)님이 2015-02-10 13:35에 작성한 댓글입니다.

 감사합니다.

초초초님이 2015-02-11 15:15에 작성한 댓글입니다. Edit
[Top]
No.
제목
작성자
작성일
조회
40718최신 데이터를 사용하는 쿼리 질문드립니다. [3]
newbnewb
2015-02-12
6916
40717통계성 화면 [2]
궁금
2015-02-11
7569
40716특정문자를 비교할려고하는데요.. [2]
질문이요
2015-02-11
7084
40715쿼리 문의드립니다. [2]
초초초
2015-02-10
7465
40711오라클 테이블 두개 COUNT 연산 관련하여 질문드립니다 [1]
최효석
2015-02-06
7639
40710plsql command window에서 프로시져 컴파일 시 & 문자 무시하고 컴파일 시키는 명령어가 있죠? [1]
궁금이
2015-02-06
7073
40709FULL OUTER JOIN 시 토드에선 돌아가는데 운영할땐 오류가 나서요.. [8]
디비잘하고싶다
2015-02-05
8526
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.018초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다