2016-09-13 5 views
0

いくつかのテーブルを結合しようとしていますが、そのうちの1つは同じテーブル上で異なる条件で同じ結合です。私はクエリを出力していますが、統一された行は出力されません。例:必要であればSQL Join Issues

select distinct rfp.id, title, bidtype, rfp.createdon as '#1', s.finishedDt as '#2', c.id as 'Contract #', supp.rfp_contr_supplier_name, es.suppdate as '#3', es2.suppdate as '#4' 
    from TBL_RFP_Ideas_NEW rfp 
left join tbl_rfp_senior s on rfp.id = s.ideaid 
left join rfp_contract c on rfp.id = c.ideaid 
inner join supplier_view supp on contractnbr = c.id 
left join TBL_EmpMaster_Full emp on rfp.sponsor_empid = emp.empid 
left join rfp_events e on rfp.id = e.ideaid 
left join rfp_events_suppliers es on e.id = es.event_id and e.heading = '4' and e.description = 'Master Agreement effective date' 
left join rfp_events_suppliers es2 on e.id = es2.event_id and e.heading = '5' and e.description = 'Master Agreement Rollout date' 
where rfp.id = '683311' 
group by rfp.id, title, bidtype, rfp.createdon, s.finishedDt, c.id, supp.rfp_contr_supplier_name, es.suppdate, es2.suppdate, e.description 

This gets output with the following Query

私はテーブルのアーキテクチャ上のいくつかを詳しく説明することができますが、ほとんどその構築された方法を説明した結合します。 Imは、不思議な小さなものを見逃したいと思っています。どんな助けもありがとう!

+1

'distinct'は' rfp.id'だけでなく、行全体で動作することをご存知ですか? –

+0

生データとあなたの結果がどのように見えるかを紹介してください。 –

+1

"group by"を使用する場合は、select文で集計関数(MinやMaxなど)も使用する必要があります。次に、集約関数で使用されているフィールドを「グループ」から削除します。あなたの "グループ"が重複を取り除いているので "DISTINCT"を削除し、 "別個"は冗長です。 –

答えて

2

データなしで伝えるのは難しい(同様rfp_events内のレコードをそのIDと一致し、おそらく3 tbl_rfp_seniorで3つのレコードがあるので、複数の行が発生している。あなたのいずれか自分のに追加の条件を追加する必要が余分を排除するために参加します(例えば、deactivation_dateがnullでないか、またはactive = 1がいくつかの考えです)、またはminやmaxなどの集計を使用して、関心のある単一の結果を取得し、それらのフィールドをグループから削除します。 group by by code smell :-)

この問題をデバッグするには、まず*を選択してグループを取り除き、複数の行がどこから来ているのかを特定してから行う必要があります凝集体。

+0

これは素晴らしい考えです。私はこれを試してみるつもりです。本当にありがとう! – slevin37

0
select distinct 
     rfp.id, title, bidtype, rfp.createdon as '#1', s.finishedDt as '#2', c.id as 'Contract #', supp.rfp_contr_supplier_name, es.suppdate as '#3', es2.suppdate as '#4' 
from 
     TBL_RFP_Ideas_NEW rfp 
     left join 
     (
      Select distinct ideaid,finishedDt 
      From tbl_rfp_senior 
     )s 
     on rfp.id = s.ideaid 
     left join 
     rfp_contract c 
     on rfp.id = c.ideaid 
     inner join 
     supplier_view supp 
     on contractnbr = c.id 
     left join 
     TBL_EmpMaster_Full emp 
     on rfp.sponsor_empid = emp.empid 
     left join 
     rfp_events e 
     on rfp.id = e.ideaid 
     left join 
     rfp_events_suppliers es 
     on e.id = es.event_id and e.heading = '4' and e.description = 'Master Agreement effective date' 
     left join 
     rfp_events_suppliers es2 
     on e.id = es2.event_id and e.heading = '5' and e.description = 'Master Agreement Rollout date' 
where rfp.id = '683311' 
group by rfp.id, title, bidtype, rfp.createdon, s.finishedDt, c.id, supp.rfp_contr_supplier_name, es.suppdate, es2.suppdate, e.description