2011-12-28 9 views
0

ここに私がしようとしていることの概念があります。2つの節を結合する

私は基本的に、現在の情報とアーカイブされた情報の2つのデータセットを持っています。私はそれらをすべて一つのリストにまとめる必要があります。

私のクエリはこれよりもはるかに複雑ですが、簡単にするために、私はちょうど私がする必要があるという一般的な考えを投稿しました。

どうすればこの機能を利用できますか?どんな助けでも大歓迎です。

(
    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

多分これは(ところで、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

    気にしないでください。とった。ちょうどエイリアスが必要でした。あなたのご親切に感謝します。 –

    1

    カップル:

    連合単一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

    +1をCTEに適用すると+1されます。 –

    関連する問題