2015-01-22 2 views
0

그래서 여기에 여러 테이블을 조인하려고합니다. 쿼리를 완성하기 위해 마지막 테이블에서 몇 개의 열을 가져와야합니다. 비록 내가이 마지막 테이블에 조인을 할 때, 나는 여러 행을 얻는다. 맨 위로 돌아 오는 것은 조인이 끝나고 맨 아래가 끝납니다.SQL 쿼리 테이블 조인, 중복 얻기

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' 

그리고 두 번째의 경우 : 여기 enter image description here

내 쿼리입니다

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

아무도 나를 도와 어떤 아이디어가 있습니까 .. 무리 더받을 수 있습니까? 필요한 경우 더 많은 정보를 표시 할 수 있습니다.

+0

UNION ALL을 원하십니까? (더 짧은 선택 목록에 null을 추가하십시오.) – jarlh

+0

올바르게 이해하면 단일 날짜 받기를 원합니다 ... 결과 세트의 prh_hist_sql (history) 테이블에서 가장 최근의 prh_hist_date (받은 날짜)를 가져오고 싶습니까? 당신은 현재 돌아오고 있습니까? – Madison

+0

UNION ALL이 어떻게 작동하는지 정확히 알지 못합니다. 지금 당장 작업 할 수 있는지 확인하기 위해 놀고 있습니다. @jarlh –

답변

0

가입 PO와 라인에 ... 그렇다면 그것! : 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' 
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') 

대신 이력 테이블에 합류의, 기준에서 지정할하고 원하는 필드를 집계 하위 쿼리를 사용 (날짜 수신)

+0

그것은 그것을 좋아하지 않는다, 그것은 prh_nbr이 첫 번째 선택에 무엇인지 말할 수있는 방법이 없다. .... 당신의 이름으로 캐럴의 멋진 그림 !!! :디 –