We help IT Professionals succeed at work.

SQL Query Syntax to find subset plus mainlist

134 Views
Last Modified: 2014-08-21
I have this query that works fine for one report but now the users want to turn it around. What this query does is find all parts from the primary vendor that are also offered from other vendors. Now what the user want is a list of all parts from the primary regardless of whether they come from another vendor plus to also list those parts that come from other vendors. In one example the current query returns 3,500 records however, there are 30,000 parts that come from this vendor. I need to include the other 26,500 parts.

Sample Data:
ITEMNMBR,VENDORID,LSTORDDT,LSORDQTY,LASTCOST
WIDGET1,VEND1,10/31/13,4,3.0
WIDGET2,VEND1,12/31/13,6,4.0
WIDGET2,VEND2,01/10/14,5,3.5

So in this case, they want to see both WIDGET1 and WIDGET2 where for WIDGET1 there are no other vendors but for WIDGET2 there is another vendor. Currently, my script will only list WIDGET2.

SELECT     V.ITEMNMBR,
                  V.VENDORID AS PRMVENDOR,
                  V.LSTORDDT AS PRMLSTORDDT,
                  V.LSORDQTY AS PRMLSORDQTY,
                  V.LAST_Originating_Cost AS PRMLASTCOST,
                  OV.VENDORID AS ALTVENDOR,
                 OV.LSTORDDT AS ALTLSTORDDT,
                 OV.LSORDQTY AS ALTLSORDQTY,
                 OV.LAST_Originating_Cost AS ALTLASTCOST
FROM      IV00103 V
                 LEFT OUTER JOIN IV00103 OV
                        ON V.ITEMNMBR = OV.ITEMNMBR
                        AND V.VENDORID <> OV.VENDORID
WHERE    V.VENDORID = @VENDORID AND V.LSTORDDT>'01/01/1901' AND OV.VENDORID IS NOT NULL AND OV.LSTORDDT>'01/01/1901'
Comment
Watch Question

Russell FoxDatabase Developer
CERTIFIED EXPERT
Top Expert 2014

Commented:
change your LEFT OUTER JOIN  to a LEFT JOIN - does that give you what you expect?
SELECT
	V.ITEMNMBR,
	V.VENDORID AS PRMVENDOR,
	V.LSTORDDT AS PRMLSTORDDT,
	V.LSORDQTY AS PRMLSORDQTY,
	V.LAST_Originating_Cost AS PRMLASTCOST,
	OV.VENDORID AS ALTVENDOR,
	OV.LSTORDDT AS ALTLSTORDDT,
	OV.LSORDQTY AS ALTLSORDQTY,
	OV.LAST_Originating_Cost AS ALTLASTCOST
FROM IV00103 V
	LEFT JOIN IV00103 OV
		ON V.ITEMNMBR = OV.ITEMNMBR
		AND V.VENDORID <> OV.VENDORID
WHERE V.VENDORID = @VENDORID 
	AND V.LSTORDDT>'01/01/1901' 
	AND OV.VENDORID IS NOT NULL 
	AND OV.LSTORDDT>'01/01/1901'

Open in new window

Senior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
rwheeler23President

Author

Commented:
This was the fix. Thanks.
rwheeler23President

Author

Commented:
Thank you.
Scott PletcherSenior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
D'OH, CORRECTION:

...
                LEFT OUTER JOIN IV00103 OV
                          ON V.ITEMNMBR = OV.ITEMNMBR
                          AND V.VENDORID <> OV.VENDORID
                          AND OV.LSTORDDT>'01/01/1901'
Scott PletcherSenior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
You're welcome!  And sorry about the copy/paste error!
rwheeler23President

Author

Commented:
No apologies necessary. Thanks for taking the time to help with something should have seen myself. You always provide top shelf responses.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.