2014-04-29 2 views
0

어떻게 이것을 왼쪽 조인 구문을 사용하여 두 개의 선택 쿼리에 결합합니까? 첫째LEFT OUTER JOIN을 사용하여 2 SELECT 쿼리 결합

select 
    * 
from 
    (select 
     mi.parent_entity_id entity, 
     tctp.institution_rec_id institutionRecId, 
     institution_code storecode, 
     institution_name storename, 
     case when sum(unsettled_points) is null 
      then coalesce (sum(point_value),0) 
      else coalesce 
     (sum(unsettled_points),0) end sumpoints 
    from 
     t_card_transaction_point tctp 
    inner join 
     m_institution mi on tctp.institution_rec_id=mi.institution_rec_id 
    where 
     mi.parent_entity_id = 70125 and 
     tctp.point_status = 'xy4604' 
    group by 
     entity, 
     institutionRecId, 
     storecode, 
     storename 
    ) storeExpired 

left join 
    entityExpired on storeExpired.entity=entityExpired.entity 

(select 
    mpb.institution_rec_id entity, 
    tctd.institution_rec_id institutionRecId, 
    tctd.card_no cardnumber, 
    total_amount_primary totalpoints, 
    case when total_unsettled_points is null 
     then point_value 
     else tctd.total_unsettled_points end 
    points 
from 
    t_card_transaction_detail tctd inner 
join 
    m_point_bucket mpb on mpb.card_no=tctd.card_no 
inner join 
    m_institution mi on mi.institution_rec_id=tctd.institution_rec_id 
where 
    mpb.total_amount_primary > 1000 and 
    tctd.adjustment_date is null 
group by 
    entity, 
    institutionRecId, 
    cardnumber, 
    totalpoints, 
    points 
) entityExpired 
+0

은 UNION 솔루션을 사용하고 있습니다. – lampdev

+0

아니요, 왼쪽 결합을 사용하여 결합해야합니다. – imAjGavz

+0

어떤 오류가 발생합니까? – Priyesh

답변

1

(내 쿼리 오류가 나는 해결책을 찾을 수 없습니다) : 우리는 가독성 :

이 두 번째의 편의를 위해 코드의 적절한 들여 쓰기/안감을 감사하십시오 는 "내 쿼리가 오류 "는 특별히 설명하지 않습니다.

SQL은

  • 선택
  • 주문을 갖는
  • 으로
  • 그룹
  • 에서

    1. 의 작동 순서가 있습니다

      됐건, 귀하의 질문에 대답하기 작성자 :

    이것은 선택이 실행될 때 별칭이 만들어 짐을 의미합니다. 그리고 "group by"가이 전에 실행되기 때문에, 별칭은 아직 존재하지 않습니다. 아마도 이것은 여러분이 얻고있는 오류 일 것입니다.

    또한 MySQL에서 별칭에 대한 조인을 허용 할지도 모르겠다.이 별칭은 쿼리에서 더 아래쪽에 정의되어 있으므로 잘못된 쿼리가 될 수 있으므로 쿼리 자체를 조인 괄호로 이동합니다. 그 후에 "on"절에서 사용하십시오.

    샘플 쿼리 : (나는 테이블을 가지고 있지 않기 때문에, 테스트되지 않음)

    select 
    * 
    from (
        select 
        mi.parent_entity_id  as entity 
        , tctp.institution_rec_id as institutionRecId 
        , institution_code   as storecode 
        , institution_name   as storename 
        , case when sum(unsettled_points) is null 
         then coalesce (sum(point_value),0) 
         else coalesce (sum(unsettled_points),0) 
        end      as sumpoints 
        from t_card_transaction_point tctp 
        inner join m_institution  mi on tctp.institution_rec_id = mi.institution_rec_id 
        where 1=1 
        and mi.parent_entity_id = 70125 
        and tctp.point_status = 'xy4604' 
        group by 
        mi.parent_entity_id 
        , tctp.institution_rec_id 
        , institution_code 
        , institution_name 
    ) storeExpired 
    left join (
        select 
        mpb.institution_rec_id as entity 
        , tctd.institution_rec_id as institutionRecId 
        , tctd.card_no    as cardnumber 
        , total_amount_primary  as totalpoints 
        , case when total_unsettled_points is null 
         then point_value 
         else tctd.total_unsettled_points 
        end      as points 
        from t_card_transaction_detail tctd 
        inner join m_point_bucket  mpb on mpb.card_no=tctd.card_no 
        inner join m_institution  mi on mi.institution_rec_id=tctd.institution_rec_id 
        where 1=1 
        and mpb.total_amount_primary > 1000 
        and tctd.adjustment_date is null 
        group by 
        mpb.institution_rec_id 
        , tctd.institution_rec_id 
        , tctd.card_no 
        , total_amount_primary 
        , case when total_unsettled_points is null 
         then point_value 
         else tctd.total_unsettled_points 
        end 
    ) entityExpired on storeExpired.entity=entityExpired.entity 
    

    편집 : 난 그냥 그것을 google'd하고 그룹에서 할 수 있습니다 실제로 사용 별칭의에 의해 (MSSQL에서는 허용되지 않으며 ANSI 표준도 아니다).

    그러나 오류에 대한 의견을 본 후 하위 쿼리가 만들어지기 전에 별칭 entityExpired와 조인하고있는 것이 원인 일 수 있습니다. 예제에서했던 것처럼 서브 쿼리를 움직이는 것이 효과가있을 것입니다.

  • +0

    안녕하세요, Gunnar, 고맙습니다.이 말이 맞는 것 같습니다. 출력이 이제 올바르게 표시됩니다. 다음 번에는 읽기 쉽고 적절한 라이닝과 들여 쓰기로 질문을 올리겠습니다 – imAjGavz

    +0

    안녕 Gunnar, 나는 그것을 받아 들일 것이다. :) – imAjGavz

    관련 문제