2017-08-28 28 views
0

我不知道如何以內部聯接從表RM001客戶名稱 - (CUSTNAM)這個查詢&已經把它添加到我的SSRS報告。可以使用幫助添加此。由於添加額外的內部聯接與多發表的SQL

「從」 saleslineitems爲「和」,但它打破了SSRS報告後,我已經試過加入。

use n 
 

 
select distinct a.[SOP Number] 
 
--, [Item Number] 
 
, a.[Customer Number], a.[Created Date from Sales Transaction], a.[Primary Shipto Address Code from Sales Line Item] 
 
, a.[City from Sales Transaction], 
 
,c.city 
 
,case 
 
    when b.CITY <> c.city then 'Cities Do Not Match' 
 
    when c.city = '' then 'Cities do not Match' 
 
    when isnull(c.city,'1') = '1' then 'Cities Do Not Match' 
 
    else '' 
 
end as [validate cities] 
 
,b.USERDEF1 as GP_F 
 
, c.f_number as EZ_F 
 
,case 
 
    when b.USERDEF1 <> c.f_number then 'Fs do not Match' 
 
    when b.USERDEF1 = '' then 'No F in GP' 
 
     
 
    else '' 
 
end as [validate Fs] 
 
, c.f_expiration 
 
,case 
 
    when c.f_expiration <= getdate() then ' F EXPIRED '  
 
    when c.f_expiration <= DATEADD(d,15,getDate()) then 'F expiring soon' 
 
    --when c.f_expiration >= dateAdd(d,61,getdate()) then 'valid F Expiration' 
 
    else '' 
 
end as [valid f date] 
 

 
--,(select top(1) c.f_number from NBS_BoundBook..contacts where c.f_number = b.userdef1 order by c.uid desc) 
 
--, a.* 
 
from SalesLineItems a 
 
inner join rm00102 b on a.[customer number] = b.CUSTNMBR and a.[Primary Shipto Address Code from Sales Line Item] = b.ADRSCODE 
 

 
left join NBS_BoundBook..contacts c on Replace(Replace(ltrim(rtrim(b.USERDEF1)),CHAR(10),''),CHAR(13),'') = 
 
(select top(1) Replace(Replace(ltrim(rtrim(c.f_number)),CHAR(10),''),CHAR(13),'') from NBS_BoundBook..contacts 
 
    where Replace(Replace(ltrim(rtrim(c.f_number)),CHAR(10),''),CHAR(13),'') = Replace(Replace(ltrim(rtrim(b.USERDEF1)),CHAR(10),''),CHAR(13),'') 
 
    and c.city= b.CITY order by c.uid desc) 
 
where [sop type] = 'Order' 
 
and [Created Date from Sales Transaction] >= dateAdd(d,-3, getDate()) 
 
and [Item Tracking Option] in ('Serial Numbers') 
 
order by a.[Customer Number]

+0

就幾點意見:在表中使用別名列。完全限定where子句過濾器列。如果任何過濾器值被從源'C'左加入將像內部聯接,並從左側產生的空值聯接將被排除,因爲NULL將不等於該過濾器的值。你應該展示你想做的事情,以便我們能夠解決你出錯的地方,並且你可以從中學習! – xQbert

回答

4

像這樣的東西應該工作:

..... 
FROM SalesLineItems a 
    INNER JOIN rm00102 b 
     ON a.[customer number] = b.CUSTNMBR 
      AND a.[Primary Shipto Address Code from Sales Line Item] = b.ADRSCODE 
    INNER JOIN rm001 cust 
    ON cust.[customer number] = a.[customer number] 
    LEFT JOIN NBS_BoundBook..contacts c 
.... 
+0

這對我來說,泰克! – Jenesis