Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Oracle - Query to filter issue

Posted on 2013-01-12
6
Medium Priority
?
261 Views
Last Modified: 2013-01-12
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


query issuequery-issue.jpg
0
Comment
Question by:Wilder1626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 38770034
you don't have a join condition between your two tables.

you are joining every location to every vendor
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 38770037
try this...


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
  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;
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38770081
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

0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 11

Author Comment

by:Wilder1626
ID: 38770129
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

0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38770142
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

0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 38770165
Thank you so much for your help.

All good now
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question