SQL Query No Results

ALL,

The query below returns all rspf.rspf_prod_n(product numbers) with barcodes (RSBB.RSBB_POS_N) only.





Select Distinct
CASE
    WHEN CHARINDEX('-',RSPF.RSPF_PROD_N) = 0 THEN LTRIM(RTRIM(RSPF.RSPF_PROD_N))
    ELSE CASE
            WHEN RSPF.RSPF_R_OR_S = 'R' THEN SUBSTRING(LTRIM(RTRIM(RSPF.RSPF_PROD_N)), 1, CHARINDEX('-',LTRIM(RTRIM(RSPF.RSPF_PROD_N)))-1)
            ELSE LTRIM(RTRIM(RSPF.RSPF_PROD_N))
    END
END AS PRODUCT_NUM,
Ltrim(Rtrim(RSBB_POS_N)) AS BarCode,
RSPF.RSPF_R_OR_S AS RENTAL_OR_SALE,
RSPF.RSPF_DESC AS ITEM_DESC,
LTRIM(RTRIM(RSPF.RSPF_CLASS)) AS CLASSNUM,
RSPC.RSPC_DESC AS CLASS_DESC,
LTRIM(RTRIM(RSPF.RSPF_GROUP_N)) AS GROUPNUM,
RSGF.RSGF_DESC AS GROUP_DESC,
RSPF.RSPF_VENDOR_N AS VENDOR_NUM,
RSPF.RSPF_VENDOR_PROD_N AS Vendor_Prod_Num,
(SELECT APVF.APVF_NAME FROM APVF WHERE APVF_VENDOR_N = RSPF.RSPF_VENDOR_N) AS VENDOR_NAME,
CASE
    WHEN RSPF.RSPF_R_OR_S = 'R' THEN ''
    ELSE CONVERT(VARCHAR, RSPF.RSPF_CREATED_DATE, 111)
END AS CREATE_DATE,
(SELECT RSPFC.RSPFC_CODE FROM RSPFC WHERE RSPFC.RSPFC_PROD_N = RSPF.RSPF_PROD_N) AS ORDER_CODE,
RSPF.RSPF_COST AS COST,
CAST(CAST(RSPF.RSPF_REPLACEMENT as numeric(7,2)) as varchar(8)) AS REPLACEMENT_COST,
RSPF.RSPF_REORDER_PRICE AS NEXT_BUY_COST,
RSPF.RSPF_LIST AS LIST_PRICE,
RSPF.RSPF_INV_Y_N
FROM RSPF, RSPC, RSGF, RSBB
WHERE RSPC.RSPC_CLASS=RSPF.RSPF_CLASS
AND RSGF.RSGF_GROUP_N = RSPF.RSPF_GROUP_N
AND RSPF.RSPF_R_OR_S = 'S'
AND RSPF.RSPF_PROD_N = RSBB.RSBB_PROD_N
GO


I need to show all product numbers even if they don't have a barcode, so I tried to do a left join on RSPF. The query just runs without ever returning anything. See below:


SELECT DISTINCT
CASE
    WHEN CHARINDEX('-',RSPF.RSPF_PROD_N) = 0 THEN LTRIM(RTRIM(RSPF.RSPF_PROD_N))
    ELSE CASE
            WHEN RSPF.RSPF_R_OR_S = 'R' THEN SUBSTRING(LTRIM(RTRIM(RSPF.RSPF_PROD_N)), 1, CHARINDEX('-',LTRIM(RTRIM(RSPF.RSPF_PROD_N)))-1)
            ELSE LTRIM(RTRIM(RSPF.RSPF_PROD_N))
    END
END AS PRODUCT_NUM,
Ltrim(Rtrim(RSBB_POS_N)) AS BarCode,
RSPF.RSPF_R_OR_S AS RENTAL_OR_SALE,
RSPF.RSPF_DESC AS ITEM_DESC,
LTRIM(RTRIM(RSPF.RSPF_CLASS)) AS CLASSNUM,
RSPC.RSPC_DESC AS CLASS_DESC,
LTRIM(RTRIM(RSPF.RSPF_GROUP_N)) AS GROUPNUM,
RSGF.RSGF_DESC AS GROUP_DESC,
RSPF.RSPF_VENDOR_N AS VENDOR_NUM,
RSPF.RSPF_VENDOR_PROD_N AS Vendor_Prod_Num,
(SELECT APVF.APVF_NAME FROM APVF WHERE APVF_VENDOR_N = RSPF.RSPF_VENDOR_N) AS VENDOR_NAME,
CASE
    WHEN RSPF.RSPF_R_OR_S = 'R' THEN ''
    ELSE CONVERT(VARCHAR, RSPF.RSPF_CREATED_DATE, 111)
END AS CREATE_DATE,
(SELECT RSPFC.RSPFC_CODE FROM RSPFC WHERE RSPFC.RSPFC_PROD_N = RSPF.RSPF_PROD_N) AS ORDER_CODE,
RSPF.RSPF_COST AS COST,
CAST(CAST(RSPF.RSPF_REPLACEMENT as numeric(7,2)) as varchar(8)) AS REPLACEMENT_COST,
RSPF.RSPF_REORDER_PRICE AS NEXT_BUY_COST,
RSPF.RSPF_LIST AS LIST_PRICE,
RSPF.RSPF_INV_Y_N
FROM RSPF, RSPC, RSGF, RSBB
Left Join RSPF A
on A.RSPF_PROD_N = RSBB.RSBB_PROD_N
WHERE RSPC.RSPC_CLASS=RSPF.RSPF_CLASS
AND RSGF.RSGF_GROUP_N = RSPF.RSPF_GROUP_N
AND RSPF.RSPF_R_OR_S = 'S'
GO



Please advise.
LVL 1
isamesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
Change your FROM and WHERE to this:
FROM RSPF
INNER JOIN RSPC ON RSPC.RSPC_CLASS=RSPF.RSPF_CLASS
INNER JOIN RSGF ON RSGF.RSGF_GROUP_N = RSPF.RSPF_GROUP_N
LEFT JOIN RSBB on RSPF.RSPF_PROD_N = RSBB.RSBB_PROD_N
WHERE RSPF.RSPF_R_OR_S = 'S'

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
isamesAuthor Commented:
Why did you Left Join RSBB instead of RSPF?
Shaun KlineLead Software EngineerCommented:
RSPF is the table containing all of your products. RSBB is the table containing barcodes for products. To show all records from RSPF and only records from RSBB that match, you need to make the left join between your main table (RSPF) to your secondary table (RSBB).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.