Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

Oracle - Query to filter issue

Hello all

I have this query bellow that validate if LOCATION.SCNALIAS exist in FP_VENDOR_INFO.VENDOR_NBR or not. It is exist, it say YES and if not, it say NO

The thing is that i get the result twice for every LOCATION.SCNALIAS. One at NO and one at YES.

It can only be YES or NO, it exist or not.

How can i fix this?

Thanks again.

SELECT distinct LOCATION.ID,
    LOCATION.SCNALIAS,
    CASE 
        WHEN LOCATION.SCNALIAS = FP_VENDOR_INFO.VENDOR_NBR
            THEN 'YES'
        ELSE 'NO'
        END SCNALIAS_MATCH
FROM TMMGR.LOCATION,
    TMMGR.FP_VENDOR_INFO
WHERE (
        SUBSTR(LOCATION.ID, 6, 1) BETWEEN 'A'
            AND ('Z')
        AND SUBSTR(LOCATION.ID, 3, 1) BETWEEN '0'
            AND ('9')
        )
    ORDER BY LOCATION.SCNALIAS ASC;

Open in new window


User generated imagequery-issue.jpg
Avatar of Sean Stuber
Sean Stuber

you don't have a join condition between your two tables.

you are joining every location to every vendor
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wilder1626

ASKER

Hi again

It looks like it work. i will just make sure i get all result.

If i want to add another NVL, i guess it would be the same process. But when i add it, on the new NVL, they all have result YES when some of them should be NO.

Do you see something wrong with this updated query?

SELECT DISTINCT LOCATION.ID,
    LOCATION.SCNALIAS,
            NVL((
                SELECT 'YES'
                FROM TMMGR.FP_VENDOR_INFO
                WHERE LOCATION.SCNALIAS = FP_VENDOR_INFO.VENDOR_NBR
                    AND ROWNUM = 1
                ), 'NO') SCNALIAS_MATCH,            
            NVL((
                SELECT 'YES'
                FROM TL_RATE TLR,
                    TL_RATE_DETAIL TRD,
                    LANE_RATE_NETWORK LRN
                WHERE TLR.CARRIER_ID = TRD.CARRIER_ID(+)
                    AND TLR.LANE_ID = TRD.LANE_ID(+)
                    AND TLR.TARIFF_CLASS_ID = TRD.TARIFF_CLASS_ID(+)
                    AND TLR.EFFECTIVE = TRD.EFFECTIVE(+)
                    AND TLR.LANE_ID = LRN.ID
                    AND TLR.CARRIER_ID = 'CUSTOMER'
                    AND LRN.FROM_KEY_NUM = 6
                    AND TLR.EXPIRATION > TO_DATE(TO_CHAR(SYSDATE, 'YYYYMMDD'), 'YYYYMMDD')
                    AND ROWNUM = 1
                ), 'NO') VENDOR_RATE_SET                
FROM TMMGR.LOCATION
WHERE (
        SUBSTR(LOCATION.ID, 6, 1) BETWEEN 'A'
            AND ('Z')
        AND SUBSTR(LOCATION.ID, 3, 1) BETWEEN '0'
            AND ('9')
        )
ORDER BY LOCATION.SCNALIAS ASC;

Open in new window

I see that i was not linking the LOCATION.ID with LRN.FROM_LOW_KEY_VALUE.

The location ID could be set in LRN.FROM_LOW_KEY_VALUE or not.

But still i have all YES when some should be NO


SELECT DISTINCT LOCATION.ID,
    LOCATION.SCNALIAS,
            NVL((
                SELECT 'YES'
                FROM TMMGR.FP_VENDOR_INFO
                WHERE LOCATION.SCNALIAS = FP_VENDOR_INFO.VENDOR_NBR
                    AND ROWNUM = 1
                ), 'NO') SCNALIAS_MATCH,            
    NVL((
            SELECT 'YES'
            FROM TL_RATE TLR,
                TL_RATE_DETAIL TRD,
                LANE_RATE_NETWORK LRN,
                LOCATION 
            WHERE TLR.CARRIER_ID = TRD.CARRIER_ID(+)
                AND TLR.LANE_ID = TRD.LANE_ID(+)
                AND TLR.TARIFF_CLASS_ID = TRD.TARIFF_CLASS_ID(+)
                AND TLR.EFFECTIVE = TRD.EFFECTIVE(+)
                AND TLR.LANE_ID = LRN.ID
                AND TLR.CARRIER_ID = 'CUSTOMER'
                AND LRN.FROM_KEY_NUM = 6
                AND LOCATION.ID = LRN.FROM_LOW_KEY_VALUE
                AND TLR.EXPIRATION > TO_DATE(TO_CHAR(SYSDATE, 'YYYYMMDD'), 'YYYYMMDD')
                AND ROWNUM = 1
                ), 'NO') VENDOR_RATE_SET                
FROM TMMGR.LOCATION
WHERE (
        SUBSTR(LOCATION.ID, 6, 1) BETWEEN 'A'
            AND ('Z')
        AND SUBSTR(LOCATION.ID, 3, 1) BETWEEN '0'
            AND ('9')
        )
ORDER BY LOCATION.SCNALIAS ASC;

Open in new window

I think i have it now.

Let me do a last validation

SELECT DISTINCT LOCATION.ID,
    LOCATION.SCNALIAS,
    CASE 
        WHEN LOCATION.SCNALIAS IS NOT NULL
            THEN '-'
        ELSE 'NO'
        END SCNALIAS_SET,
            NVL((
                SELECT '-'
                FROM TMMGR.FP_VENDOR_INFO
                WHERE LOCATION.SCNALIAS = FP_VENDOR_INFO.VENDOR_NBR
                    AND ROWNUM = 1
                ), 'NO') SCNALIAS_MATCH,            
    NVL((
            SELECT '-'
            FROM TL_RATE TLR,
                TL_RATE_DETAIL TRD,
                LANE_RATE_NETWORK LRN
            WHERE TLR.CARRIER_ID = TRD.CARRIER_ID(+)
                AND TLR.LANE_ID = TRD.LANE_ID(+)
                AND TLR.TARIFF_CLASS_ID = TRD.TARIFF_CLASS_ID(+)
                AND TLR.EFFECTIVE = TRD.EFFECTIVE(+)
                AND TLR.LANE_ID = LRN.ID
                AND TLR.CARRIER_ID = 'CUSTOMER'
                AND LRN.FROM_KEY_NUM = 6
                AND LRN.FROM_LOW_KEY_VALUE (+) = LOCATION.ID
                AND TLR.EXPIRATION > TO_DATE(TO_CHAR(SYSDATE, 'YYYYMMDD'), 'YYYYMMDD')
                AND ROWNUM = 1
                ), 'NO') VENDOR_RATE_SET                
FROM TMMGR.LOCATION
WHERE (
        SUBSTR(LOCATION.ID, 6, 1) BETWEEN 'A'
            AND ('Z')
        AND SUBSTR(LOCATION.ID, 3, 1) BETWEEN '0'
            AND ('9')
        )
ORDER BY LOCATION.SCNALIAS ASC;

Open in new window

Thank you so much for your help.

All good now