2014-08-27 4 views
0

SQL에서 경험이 없으므로 비교를 위해 여러 테이블에 여러 계층 필터를 적용하는 쿼리가 필요합니다.SQL 쿼리 여러 테이블에 여러 필터 적용

표는 :

ORDER_NUMBER STEP_NAME 데이터 PARAMETER_NAME
12 STEP4은 P1
12 STEP4 const2 P2
12 STEP4 VALUE1 P3
30 STEP6가 P1
30 STEP6는 P2
30 STEP6에 값 2의 P3을 const4 const3 const1

모든 테이블의 형식은 동일하지만 데이터 값이 다릅니다. 주문 번호는 테이블마다 다를 수 있습니다.

내가하고 싶은 : 사이

  1. 검색 행 STEP_NAME = 1 단계와 step8 만
  2. 찾기 ORDER_NUMBER 매칭 (P1 = const1 및 P2 = const2) 같은 순서로 번호
  3. 저장 P3 행 그룹.
  4. 표시 모든 행 (각 테이블에서 하나) :

ORDER_NUMBER 테이블 데이터 PARAMETER_NAME
12 표 값 1 P3
12 표 2 값 2 P3
14 표 3 VALUE3 P3

당신의 도움이 크게이다 고맙다!

+0

당신이 '멀티 레이어 필터'로 무엇을 의미합니까? – Horaciux

답변

0

당신은 필요한만큼 union all...select 부품을 추가해야합니다 :

이 이
select 
    order_number, 
    'table1' as `table`, 
    data, 
    prameter_name 
from 
    table1 t1p3 
where 
    step_name in ('step1', 'step2', 'step3', 'step4', 'step5', 'step6', 'step7', 'step8') and 
    parameter = 'p3' and 
    exists (
     select 
      'x' 
     from 
      table1 t1p1 
     where 
      t1p1.order_number = t1p3.order_number and 
      t1p1.step_name = t1p3.step_name and 
      t1p1.parameter = 'p1' and 
      t1p1.data = 'const1' 
    ) and exists (
     select 
      'x' 
     from 
      table1 t1p2 
     where 
      t1p2.order_number = t1p3.order_number and 
      t1p2.step_name = t1p3.step_name and 
      t1p2.parameter = 'p2' and 
      t1p2.data = 'const2' 
    ) 
union all 
select 
    order_number, 
    'table2', 
    data, 
    prameter_name 
from 
    table2 t2p3 
where 
    step_name in ('step1', 'step2', 'step3', 'step4', 'step5', 'step6', 'step7', 'step8') and 
    parameter = 'p3' and 
    exists (
     select 
       'x' 
     from 
      table2 t2p1 
     where 
      t2p1.order_number = t2p3.order_number and 
      t2p1.step_name = t2p3.step_name and 
      t2p1.parameter = 'p1' and 
      t2p1.data = 'const1' 
    ) and exists (
     select 
      'x' 
     from 
      table2 t2p2 
     where 
      t2p2.order_number = t2p3.order_number and 
      t2p2.step_name = t2p3.step_name and 
      t2p2.parameter = 'p2' and 
      t2p2.data = 'const2' 
    ) 
+0

고마워요. 나는 당신의 계단을 따라 갔고 특정 값에서 P1과 P2로 order_number를 찾는 더 작은 규모로 성공했습니다. – AnneZ

관련 문제