[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

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.
0
rwheeler23
Asked:
rwheeler23
2 Solutions
 
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
 
PortletPaulCommented:
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now