SQL连接问题

SQL连接问题,sql,sql-server,join,Sql,Sql Server,Join,我正在尝试将几个表连接在一起,其中一个是同一个表上的相同连接,条件不同。我在查询中得到输出,但不是在一条统一的线上。例如: 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' fr

我正在尝试将几个表连接在一起,其中一个是同一个表上的相同连接,条件不同。我在查询中得到输出,但不是在一条统一的线上。例如:

    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

如果需要的话,我可以详细介绍一些表体系结构,但是连接基本上解释了它是如何构建的。我希望我错过了一些非常小的东西。感谢您的帮助

之所以出现多行,是因为在tbl_rfp_senior中有3条记录与该id匹配,而且在rfp_事件中可能有3条记录,如果没有数据也很难判断。您需要向联接中添加其他条件以消除多余的行,例如,停用日期不为空?或active=1是一些想法,或使用min和max或其他聚合(如sum)来获得感兴趣的单个结果,并从分组中删除这些字段。我还建议,如果distinct和group by都存在,则存在代码气味:-


为了帮助调试这个问题,我将首先选择*并通过识别多行来自何处以及您需要在何处进行聚合来摆脱组。

之所以出现多行,是因为tbl\u rfp\u senior中有3条记录与该id匹配,而且可能还有3条rfp\u事件记录也很难分辨没有数据。您需要向联接中添加其他条件以消除多余的行,例如,停用日期不为空?或active=1是一些想法,或使用min和max或其他聚合(如sum)来获得感兴趣的单个结果,并从分组中删除这些字段。我还建议,如果distinct和group by都存在,则存在代码气味:-

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

为了帮助调试此问题,我将首先选择*并通过确定多行的来源和需要进行聚合的位置来删除组。

您是否知道distinct操作的是整行,而不仅仅是rfp.id?如果要使用group,请提一下您的原始数据和结果如何到那时,您还应该在select语句中使用一些聚合函数,如Min或Max。然后从group by中删除聚合函数中使用的字段。然后删除DISTINCT,因为您的group by正在删除重复项,DISTINCT是冗余的。为什么在没有使用聚合函数的情况下使用group by?我知道DISTINCT在整行上运行,是的。我试图使用distinct Original来清除我返回的空值。问题是3和4的日期在那里,但不会作为一行输出,而是作为多行输出,似乎没有差异。这是因为group by吗?您是否知道distinct对整行进行操作,而不仅仅是对rfp.id进行操作?请提及您的原始数据以及结果如何?如果您要使用group by,那么您还应该在select语句中使用一些聚合函数,如Min或Max。然后从group by中删除聚合函数中使用的字段。然后删除DISTINCT,因为您的group by正在删除重复项,DISTINCT是冗余的。为什么在没有使用聚合函数的情况下使用group by?我知道DISTINCT在整行上运行,是的。我试图使用distinct Original来清除我返回的空值。问题是3和4的日期在那里,但不会作为一行输出,而是作为多行输出,似乎没有差异。这是因为小组讨论吗?这是个好主意。我要试试这个。非常感谢!这是个好主意。我要试试这个。非常感谢!
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