2011-12-28 4 views
0

다음은 내가하려는 일의 개념입니다.두 개의 절을 합치는 것

기본적으로 두 세트의 데이터가 있습니다. 하나는 현재 정보 용이고 다른 하나는 보관 정보 용입니다. 나는 그들 모두를 하나의리스트에 보여주기 위해 그들을 결합시켜야한다.

내 쿼리는 이보다 훨씬 복잡하지만 간단하게하기 위해 내가해야 할 일에 대한 일반적인 아이디어를 게시했습니다.

어떻게하면 효과가 있습니까? 어떤 도움이라도 대단히 감사합니다.

(
    with m1 as 
    (
     select --distinct 
     pfa.FacilityID 
     from PatientFormCompleted 
    ) 
    select * from m1 
    left join other tables 
) 
union all 
(
    with m1archive as 
    (
     select --distinct 
     pfa.FacilityID 
     from PatientFormArchive 
    ) 
    select * from m1archive 
    left join other tables 
) 

답변

2

가능성이 종료됩니다 (자신 BTW,을 downvoted하지, 투표)하지만 편집 후, 당신은 질문에 어떤 노력을 넣어 않았다, 그래서 여기에

당신이 사용할 수있는가는 여러 CTE의 만

  • 들이 첫 번째 WITH
작성됩니다 서로
  • 를 따르는 제약 조건

    SELECT *을 사용하지 말고 반환 할 열을 구체적으로 지정하십시오. 옵션

    SQL 문

    with m1 as 
    (
        select --distinct 
        pfa.FacilityID 
        from PatientFormCompleted 
    ) 
    , m1archive as 
    (
        select --distinct 
        pfa.FacilityID 
        from PatientFormArchive 
    ) 
    select * from m1 
    left join other tables 
    union all 
    select * from m1archive 
    left join other tables 
    
  • +0

    도와 주셔서 정말 감사합니다. 그것은 내가 필요한 것입니다. –

    +0

    후속 질문이 있습니다. 모든 레코드가 하나의 합계로 계산되도록하려면 어떻게해야합니까? –

    +1

    신경 쓰지 마라. 알았다. 별칭이 필요했습니다. 모든 도움을 주셔서 다시 한 번 감사드립니다. –

    1

    커플 :

    연합 (EU) 단일 CTE로 :

    with m1 as 
    (
        select --distinct 
        pfa.FacilityID 
        from PatientFormCompleted 
    
        union all 
    
        select --distinct 
        pfa.FacilityID 
        from PatientFormArchive 
    
    ) 
    select * from m1 
    left join other tables 
    

    사용 하나 이상의 CTE :

    with m1 as 
    (
        select --distinct 
        pfa.FacilityID 
        from PatientFormCompleted 
    ) 
    ,m1archive as 
    (
        select --distinct 
        pfa.FacilityID 
        from PatientFormArchive 
    ) 
    select * from m1 
    left join other tables 
    union all 
    select * from m1archive 
    left join other tables 
    
    +0

    CTE에서 노동 조합 적용시 +1. –

    관련 문제