Sql Server-左联接缺少记录
当只为Asset_Key=4744运行一个块时,我得到了2行,但当我离开join并返回B最终结果时,只为Asset_Key=4744返回了1行 但是如果我取消注释A.Asset_Key=4744,我将得到两条记录 请帮忙Sql Server-左联接缺少记录,sql,sql-server,Sql,Sql Server,当只为Asset_Key=4744运行一个块时,我得到了2行,但当我离开join并返回B最终结果时,只为Asset_Key=4744返回了1行 但是如果我取消注释A.Asset_Key=4744,我将得到两条记录 请帮忙 SELECT A.Asset_Key,Asset_Type, A.Reason_Desc,A.NotReportingDate,A.ReportedEndDate,NULL AS Actual_ReportedDate,A.Asset_ID,A.CompanyCode --,
SELECT A.Asset_Key,Asset_Type,
A.Reason_Desc,A.NotReportingDate,A.ReportedEndDate,NULL AS Actual_ReportedDate,A.Asset_ID,A.CompanyCode
--,A.OEM_Key,A.Job_Code,A.Job_Desc,A.IC_Desc,A.ReportedDate
FROM
(
SELECT F.Asset_Key,F.Asset_Type,
F.Reason_Desc,F.NotReportingDate,F.ReportedEndDate,NULL AS Actual_ReportedDate,F.Asset_ID,F.CompanyCode
FROM
(
SELECT Job_Code,Job_Desc,IC_Desc,Asset_Type,Reason_Desc,Asset_ID,CompanyCode,OEM_Key,
NotReportingDate,ISNULL(ReportedDate,'2017-06-30') AS ReportedEndDate,ReportedDate,
ROW_NUMBER() OVER(PARTITION BY Asset_ID,CompanyCode,OEM_Key,ReportedDate ORDER BY RecordCreateTimestamp DESC) As Rank,
Asset_Key
FROM
(
SELECT Job_Code,Job_Desc,IC_Desc,AJT.Asset_Type,DN.Reason_Desc,AJT.Asset_ID,AJT.CompanyCode,AJT.OEM_Key,
NotReportingDate,FN.ReportedDate,FN.Asset_Key,FN.RecordCreateTimestamp
FROM F_NOTREPORTING_REASON_TRACKING(NOLOCK) FN
JOIN D_NOTREPORTING_REASON(NOLOCK) DN ON FN.NotReportingReason_Key=DN.NotReportingReason_Key
JOIN D_ASSET_JOB_TRACKING (NOLOCK) AJT ON AJT.Asset_Key=FN.Asset_Key AND AJT.Is_Active_flag='Y'
WHERE CONVERT(VARCHAR(6),ReportedDate,112)=201706
AND CONVERT(VARCHAR(6),FN.NotReportingDate,112)<=201706
AND AJT.Asset_Type NOT IN('Concrete Batching Plants')
--AND FN.ReportedDate>=FN.NotReportingDate
UNION
SELECT E.Job_Code,E.Job_Desc,E.IC_Desc,E.Asset_Type,E.Reason_Desc,E.Asset_ID,E.CompanyCode,E.OEM_Key,
E.NotReportingDate,E.ReportedDate,E.Asset_Key,
E.RecordCreateTimestamp
FROM
(
SELECT Job_Code,Job_Desc,IC_Desc,AJT.Asset_Type,DN.Reason_Desc,AJT.Asset_ID,AJT.CompanyCode,AJT.OEM_Key,
NotReportingDate,
ISNULL(LEAD(FN.NotReportingDate,1) OVER(PARTITION BY AJT.Asset_ID,AJT.CompanyCode,AJT.OEM_Key ORDER BY AJT.RecordCreateTimestamp DESC),'2017-06-30') AS ReportedDate,FN.Asset_Key,
FN.RecordCreateTimestamp
FROM F_NOTREPORTING_REASON_TRACKING(NOLOCK) FN
JOIN D_NOTREPORTING_REASON(NOLOCK) DN ON FN.NotReportingReason_Key=DN.NotReportingReason_Key
JOIN D_ASSET_JOB_TRACKING (NOLOCK) AJT ON AJT.Asset_Key=FN.Asset_Key AND AJT.Is_Active_flag='Y'
WHERE FN.ReportedDate IS NULL
AND CONVERT(VARCHAR(6),FN.NotReportingDate,112)<=201706
AND AJT.Asset_Type NOT IN('Concrete Batching Plants')
)E
WHERE (CONVERT(VARCHAR(6),E.ReportedDate,112)=201706)
)C
)F
--WHERE A.Asset_Key is null
)A
LEFT OUTER JOIN
(
SELECT AU1.Asset_Key,D.Full_Date
FROM D_DATE D
JOIN VW_IOT_ANALYTICS_DASHBOARDAllDates AU1(NOLOCK) ON AU1.Date_Key=D.Date_Key
JOIN D_ASSET_JOB_TRACKING AJT1(NOLOCK) ON AU1.Asset_Key=AJT1.Asset_Key AND AJT1.Is_Active_flag='Y'
WHERE CONVERT(VARCHAR(6),D.Full_Date,112)=201706
AND AJT1.Asset_Type NOT IN('Concrete Batching Plants')
)B ON A.Asset_Key=B.Asset_Key --AND (B.Full_Date BETWEEN A.NotReportingDate AND A.ReportedEndDate)
--where A.Asset_ID='01180363'
--where a.asset_id like '0118036%'
--where A.Asset_Key=4744
order by a.Asset_Key
或者,当您只运行块A时,A.Asset_Key是null检查Asset_Key value?我不明白您能否简单解释一下为什么要将日期转换为varchar6,然后将结果与整数值而不是字符串值进行比较?非主题:其中CONVERTVARCHAR6,E.ReportedDate,112=201706是处理日期/时间信息的一种不好的方式,将数据bad转换为varcharslow,并与integer bad进行比较。不要更改数据以适合您的过滤器,请执行相反的操作,例如e.ReportedDate>='201706'和e.ReportedDate<'201707'