SQL Query Syntax to find subset

This query returns all the parts that are supplied by a vendor. The PK here is ITEMNMBR,VENDORID
select ITEMNMBR,VENDORID,LSTORDDT,LSORDQTY,LASTCOST
FROM MyItemTable
where VENDORID='MyVendorID'

How do I structure a query to return for each part, other vendors that supply that part and the same fields?

So if vendor ACME supplies a WIDGET but so do vendors BUGS and BUNNY, I want to see two records showing the last order date, quantity and cost for ACME compared to the last order date, quantity and cost from BUGS and BUNNY.
LVL 1
rwheeler23Asked:
Who is Participating?
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.

Ioannis ParaskevopoulosCommented:
SELECT     V.ITEMNMBR,
                  V.VENDORID,
                  V.LSTORDDT,
                  V.LSORDQTY,
                  V.LASTCOST,
                 OV.LSTORDDT,
                 OV.LSORDQTY,
                 OV.LASTCOST
FROM      MyItemTable V
                 LEFT JOIN MyItemTable OV
                        ON V.ITEMNMBR = OV.ITEMNMBR
                        AND V.VENDORID <> OV.VENDORID
WHERE    V.VENDORID = 'MyVendorID'
ORDER BY V.ITEMNMBR

Open in new window


Giannis
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
The following query doesn't give you the result you need?

SELECT ITEMNMBR,VENDORID,LSTORDDT,LSORDQTY,LASTCOST
 FROM MyItemTable
ORDER BY ITEMNMBR,VENDORID,LSTORDDT,LSORDQTY,LASTCOST

Open in new window

0
PortletPaulfreelancerCommented:
This sample result
| ITEMNMBR | ALTITEM | VENDORID | ALTVEND |   LSTORDDT |      ALTDT | LSORDQTY | ALTQTY | LASTCOST | ALTCOST | SUP_UNITCOST | ALT_UNITCOST |
|----------|---------|----------|---------|------------|------------|----------|--------|----------|---------|--------------|--------------|
|        1 |       1 |     acme |    bugs | 2014-01-01 | 2013-10-11 |      120 |    100 |      120 |     120 |            1 |          1.2 |
|        1 |       1 |     acme |   bunny | 2014-01-01 | 2013-08-12 |      120 |    200 |      120 |     300 |            1 |          1.5 |

Open in new window

produced by
with lastords as (

  select
        itemnmbr
      , vendorid
      , lstorddt
      , lsordqty
      , lastcost
      , row_number() over (partition by itemnmbr, vendorid 
                           order by lstorddt DESC)         as rn
  from Table1
  )
select
        sup.itemnmbr
      , alt.itemnmbr altitem
      , sup.vendorid
      , alt.vendorid altvend
      , sup.lstorddt
      , alt.lstorddt altdt
      , sup.lsordqty
      , alt.lsordqty altqty
      , sup.lastcost
      , alt.lastcost altcost
      , sup.lastcost / sup.lsordqty sup_unitcost
      , alt.lastcost / alt.lsordqty alt_unitcost
from lastords as sup --plier
left join lastords as alt --ternate
      on  sup.itemnmbr = alt.itemnmbr
      and sup.vendorid <>  alt.vendorid
      and alt.rn = 1
where sup.vendorid = 'acme'
and sup.rn = 1

Open in new window


TIP:
Provide "sample data" and "expected result", this entices people to propose solutions, and makes it more relevant to you.

see: http://sqlfiddle.com/#!3/8fa30/10
0

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
rwheeler23Author Commented:
These two versions work but know I have been asked to expand it to include other tables. This was a big help in getting me started.  For your information, what they want is to be able to see are all the items that come from the specified vendor but then also see any other vendor from which this part comes. What I now need to do is get the PO's on both sides so they can start doing a cost comparison.
0
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 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.