我正在尝试将几个表连接在一起,其中一个表是同一个表中的相同连接,具有不同的条件。我在查询中获得输出,但不是在一个统一的行上。例如:
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
如果需要,我可以详细说明表架构,但是连接几乎可以解释它是如何构建的。我希望我错过了一些非常小的东西。任何帮助表示赞赏!
答案 0 :(得分:2)
正在发生多行,因为tbl_rfp_senior
中有3条记录与该ID匹配,并且rfp_events
中可能还有3条记录(没有数据很难分辨。您需要添加其他条件你的连接消除多余的行(例如,deactivation_date不是空的?或者active = 1是一些想法)或使用min和max或其他一些像sum这样的聚合来获得感兴趣的单个结果并从分组中删除这些字段.I还会建议,如果存在distinct和group by,则存在代码气味: - )
为了帮助调试此问题,我首先选择*并删除组,并确定多行来自何处以及需要进行聚合的位置。
答案 1 :(得分: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