2016-09-13 2 views
0

몇 가지 테이블을 함께 조인하려고합니다. 그 중 하나는 동일한 테이블에서 다른 조건으로 조인됩니다. 내 쿼리에서 출력을 얻지 만 하나의 통합 된 라인에서는 출력하지 않습니다. 예 : 필요하다면SQL 가입 문제

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는 미친 듯이 작은 것을 놓치기를 바래다. 어떤 도움을 주셔서 감사합니다! 아마 rfp_events 3 개 기록뿐만 아니라 (하드 데이터없이 말해. 당신이 하나가 당신의 여분을 제거하기 위해 조인에 추가 기준을 추가 할 필요가 3 개 그 ID와 일치 tbl_rfp_senior에 기록하고 있기 때문에

+1

'distinct'는'rfp.id'뿐만 아니라 전체 행에서 작동한다는 것을 알고 있습니까? –

+0

원시 데이터 및 결과가 어떻게 나타나는지 언급 해주십시오. –

+1

"group by"을 사용하려는 경우 select 문에서 일부 또는 전체 집계 함수 (예 : Min 또는 Max)를 사용해야합니다. 그런 다음 "group by"에서 집계 함수에 사용 된 필드를 제거하십시오. 그런 다음 "그룹 별"이 중복을 제거하고 "별개"가 중복되므로 "DISTINCT"를 제거하십시오. –

답변

2

다중 행이 발생하는 행 (예 : deactivation_date가 null이 아니거나 active = 1이 어떤 생각입니다) 또는 min과 max 또는 sum과 같은 다른 집계를 사용하여 관심있는 단일 결과를 가져 와서 해당 필드를 그룹에서 제거 할 수 있습니다. group by 모두 코드 냄새가 있습니다 :-)

이 문제를 디버깅하려면 *를 선택하고 그룹을 제거하고 여러 행의 출처 및 수행해야 할 위치를 식별하십시오. 집합체.

+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