Sql Server-左连接丢失记录

时间:2017-07-04 05:06:58

标签: sql sql-server

当仅运行Asset_Key = 4744的A块时,我得到2行但是当我离开加入B的最终结果时,仅返回1行Asset_Key = 4744 但如果我取消注释(其中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
--,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

0 个答案:

没有答案