使用多表SQL添加额外的内连接

时间:2017-08-28 15:06:21

标签: sql reporting-services inner-join ssrs-2012

我想知道如何将表rm001中的客户名称(CUSTNAM)加入此查询&将它添加到我的SSRS报告中。可以使用帮助添加这个。感谢

我尝试在“from”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] 

1 个答案:

答案 0 :(得分: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
....