SQL查询连接表,获取重复

时间:2015-01-22 15:38:30

标签: sql left-join

所以我想在这里加入多个表格。我需要从上一个表中获取几列以完成查询。虽然,当我在最后一个表上进行连接时,我最终获得了多行。顶部返回是在连接之后,底部是在之前。 enter image description here

以下是我的疑问:

SELECT 
prh_nbr as 'PO', 
ad_name as 'Supplier', 
pt_part as 'Part Number', 
pt_desc1 as 'Part Description', 
prh_rcvd as 'Qty Received', 
CAST(prh_qty_ord as int) as 'Quantity Ordered', 
prh_pur_cost as 'Purchase Cost', 
(prh_pur_cost * prh_rcvd) as 'Total Cost', 
CAST(e.prh_rcp_date As Date) As 'Date Received' 
    from qad_repl.dbo.pod_det_sql as a
    LEFT OUTER JOIN qad_repl.dbo.po_mstr_sql as b ON (a.pod_nbr = b.po_nbr)
    LEFT OUTER JOIN qad_repl.dbo.ad_mstr_sql as c ON (b.po_vend = c.ad_addr)
    LEFT OUTER JOIN qad_repl.dbo.pt_mstr_sql as d ON (a.pod_part = d.pt_part)
    LEFT OUTER JOIN qad_repl.dbo.prh_hist_sql as e ON (po_nbr = e.prh_nbr)
    where pt_part is not null and po_nbr is not null and pod_qty_rcvd > 0
    and prh_nbr = 'PO1701' and prh_rcp_date >= '2015-01-21' and prh_rcp_date <= '2015-01-22'

然后是第二个:

SELECT 
pod_nbr as 'PO', 
ad_name as 'Supplier', 
pt_part as 'Part Number', 
pt_desc1 as 'Part Description'
       from qad_repl.dbo.pod_det_sql as a
    LEFT OUTER JOIN qad_repl.dbo.po_mstr_sql as b ON (a.pod_nbr = b.po_nbr)
    LEFT OUTER JOIN qad_repl.dbo.ad_mstr_sql as c ON (b.po_vend = c.ad_addr)
    LEFT OUTER JOIN qad_repl.dbo.pt_mstr_sql as d ON (a.pod_part = d.pt_part)
    where pt_part is not null and po_nbr is not null and pod_qty_rcvd > 0
    and pod_nbr = 'PO1701'

我不确定您需要/希望看到多少信息,但我想尽可能保持清洁。

当我尝试加入我的最后一个表(prh_hist_sql)时,似乎我只能加入PO编号和行号。在上面的例子中我做了po_nbr,这就是为什么我得到的是四行而不是两行...如果我做行号,我会得到更多,如图所示..

enter image description here

有没有人有任何想法可以帮助我? 如果需要,我可以显示更多信息,只需询问。

2 个答案:

答案 0 :(得分:0)

这可能有用......

SELECT prh_nbr as 'PO', ad_name as 'Supplier', pt_part as 'Part Number', pt_desc1 as 'Part Description', prh_rcvd as 'Qty Received', 
CAST(prh_qty_ord as int) as 'Quantity Ordered', prh_pur_cost as 'Purchase Cost'
    , (prh_pur_cost * prh_rcvd) as 'Total Cost'
    , (select max(prh_rcp_date) from prh_hist_sql where prh_nbr = b.po_nbr and prh_rcp_date >= '2015-01-21' and prh_rcp_date <= '2015-01-22') 'Date Received' 
from qad_repl.dbo.pod_det_sql as a
LEFT OUTER JOIN qad_repl.dbo.po_mstr_sql as b ON (a.pod_nbr = b.po_nbr)
LEFT OUTER JOIN qad_repl.dbo.ad_mstr_sql as c ON (b.po_vend = c.ad_addr)
LEFT OUTER JOIN qad_repl.dbo.pt_mstr_sql as d ON (a.pod_part = d.pt_part)
where pt_part is not null and po_nbr is not null and pod_qty_rcvd > 0
    and prh_nbr = 'PO1701' 
    and exists(select 1 from prh_hist_sql where prh_nbr = b.po_nbr and prh_rcp_date >= '2015-01-21' and prh_rcp_date <= '2015-01-22')

使用子查询来指定条件并聚合所需的字段(收到日期),而不是加入历史表

答案 1 :(得分:0)

我想通了,加入PO和Line ......然后区分它! :d

SELECT DISTINCT pod_nbr as 'PO', ad_name as 'Supplier', pt_part as 'Part Number', pt_desc1 as 'Part Description'
   from qad_repl.dbo.pod_det_sql as a
LEFT OUTER JOIN qad_repl.dbo.po_mstr_sql as b ON (a.pod_nbr = b.po_nbr)
LEFT OUTER JOIN qad_repl.dbo.ad_mstr_sql as c ON (b.po_vend = c.ad_addr)
LEFT OUTER JOIN qad_repl.dbo.pt_mstr_sql as d ON (a.pod_part = d.pt_part)
LEFT OUTER JOIN qad_repl.dbo.prh_hist_sql as e ON (po_nbr = e.prh_nbr and pod_line = e.prh_line)
where pt_part is not null and po_nbr is not null and pod_qty_rcvd > 0
and pod_nbr = 'PO1701'