I'm trying to retrieve data from three different tables.
Table1KIT_CONT
- holds kit unique id, kit content part numbers and kit revision among others.
KIT_CONT_ID | KIT_CONT_PN | KIT_CONT_REV | KIT_CONT_QTY |
---|---|---|---|
1 | ABC | 1 | 2 |
1 | DEF | 1 | 1 |
2 | GHI | 1 | 3 |
2 | ABC | 1 | 4 |
3 | ABC | 2 | 5 |
...
Table2KIT_PN
- holds Kit Unique id, Kit Part number and kit revision among others
KIT_ID | KIT_PN | KIT_REV |
---|---|---|
1 | KIT-1 | 1 |
2 | KIT-2 | 1 |
3 | KIT-1 | 2 |
4 | KIT-3 | 1 |
...
Table3PN_PRICE
- holds Part Number, Part Price, Pricelist rev among others
PN | PN_PRICE | PRICELIST_REV |
---|---|---|
ABC | 500 | 1 |
DEF | 700 | 1 |
GHI | 900 | 1 |
ABC | 550 | 2 |
DEF | 750 | 2 |
GHI | 950 | 2 |
...
I need a query that returns:
KIT_PN | KIT_CONT_PN | KIT_CONT_QTY | KIT_REV | PN_PRICE | PRICELIST_REV |
---|
join all KIT_CONT PN to the KIT_PN ON ID number where kit rev is the highest for each kit join the PN_Price on PN = KIT_CONT_PN where pricelist rev is highest for each PN
Select
A.KIT_ID
A.KIT_PN,
B.KIT_CONT_PN,
B.KIT_CONT_QTY,
C.PN_PRICE,
C.PN_PRICELIST_REV
FROM Table2KIT_PN A WITH (NOLOCK)
left JOIN
Table1KIT_CONT B WITH (NOLOCK)
ON A.KIT_ID = B.KIT_CONT_ID
left JOIN
Table3PN_PRICE C WITH (NOLOCK)
On B.KIT_CONT_PN = C.PN
and B.KIT_CONT_REV = (Select Max(B.KIT_CONT_REV) FROM Table1KIT_CONT D where B.KIT_CONT_REV = D.KIT_CONT_REV )
This does not work at all. And I haven't even got to the part of only retrieving the max PN_PRICE.
I have tried multiple versions of queries found in this link, but cannot seem to wrap all of this info to my case.
Any help is much appreciated.