SELECT 
    CASE 
        WHEN 
        (
            SELECT COUNT(*)
            FROM T_SAL_DELIVERYNOTICEENTRY A
            INNER JOIN T_SAL_DELIVERYNOTICEENTRY_LK B ON A.FENTRYID = B.FENTRYID AND B.FSTABLENAME = 'T_SAL_ORDERENTRY'
            INNER JOIN T_SAL_ORDER C ON C.FID = B.FSBILLID
            INNER JOIN T_SAL_ORDERENTRY D ON C.FID = D.FID
            WHERE C.FSaleOrgId = 100302
            AND C.FSALEORGID <> C.F_SaleOrgId 
            AND A.FENTRYID = t1.FENTRYID
        ) > 0 
        THEN 1
        WHEN(
            SELECT COUNT(*)
            FROM T_SAL_DELIVERYNOTICEENTRY A
            INNER JOIN T_SAL_DELIVERYNOTICEENTRY_LK B ON A.FENTRYID = B.FENTRYID AND B.FSTABLENAME = 'T_SAL_ORDERENTRY'
            INNER JOIN T_SAL_ORDER C ON C.FID = B.FSBILLID
            INNER JOIN T_SAL_ORDERENTRY D ON C.FID = D.FID
            WHERE C.FSaleOrgId <> 100302
            AND A.FENTRYID = t1.FENTRYID
        ) > 0
        THEN 1
        ELSE 
        (
            SELECT COUNT(*)
            FROM T_PRD_MO A
            INNER JOIN T_PRD_MOENTRY B ON A.FID = B.FID
            INNER JOIN T_PRD_MOENTRY_LK C ON B.FENTRYID = C.FENTRYID AND C.FSTABLENAME = 'T_SAL_ORDERENTRY'
            WHERE A.FDocumentStatus = 'C'
            AND C.FSBILLID IN 
            (
                SELECT C.FID
                FROM T_SAL_DELIVERYNOTICEENTRY A
                INNER JOIN T_SAL_DELIVERYNOTICEENTRY_LK B ON A.FENTRYID = B.FENTRYID AND B.FSTABLENAME = 'T_SAL_ORDERENTRY'
                INNER JOIN T_SAL_ORDER C ON C.FID = B.FSBILLID
                INNER JOIN T_SAL_ORDERENTRY D ON C.FID = D.FID
                WHERE C.FSaleOrgId = 100302
                AND C.FSALEORGID = C.F_SaleOrgId 
                AND A.FENTRYID = t1.FENTRYID
            )
        )
    END AS Result