SQL 查询连接表,获取重复项
SQL Query Joining Tables, Getting Duplicate
所以我想在这里加入多个 table。我需要从我的最后一个 table 中抓取几列来完成查询。虽然,当我对最后一个 table 进行连接时,我最终得到了多行。顶部 return 是连接之后,底部是连接之前。
这是我的查询:
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'
我不确定您 need/want 会看到多少信息,但我会尽量保持信息的清晰。
当尝试加入我的最后一个 table (prh_hist_sql) 时,我似乎只能加入 PO 编号和行号。在上面的示例中,我做了 po_nbr,这这就是为什么我得到四行而不是两行的原因......如果我做行号我会得到更多,如图所示..
有没有人有什么想法可以帮助我?
如果需要我可以显示更多信息,请问。
这可能有用...
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')
不要加入历史记录 table,而是使用子查询来指定条件并聚合所需的字段(接收日期)
我想通了,加入 PO AND 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'
所以我想在这里加入多个 table。我需要从我的最后一个 table 中抓取几列来完成查询。虽然,当我对最后一个 table 进行连接时,我最终得到了多行。顶部 return 是连接之后,底部是连接之前。
这是我的查询:
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'
我不确定您 need/want 会看到多少信息,但我会尽量保持信息的清晰。
当尝试加入我的最后一个 table (prh_hist_sql) 时,我似乎只能加入 PO 编号和行号。在上面的示例中,我做了 po_nbr,这这就是为什么我得到四行而不是两行的原因......如果我做行号我会得到更多,如图所示..
有没有人有什么想法可以帮助我? 如果需要我可以显示更多信息,请问。
这可能有用...
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')
不要加入历史记录 table,而是使用子查询来指定条件并聚合所需的字段(接收日期)
我想通了,加入 PO AND 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'