Jump to content

Recommended Posts

Using a stored procedure on SQL Server.

 

Here are my tables:

 

Families

Groups

Lines

SubLines

Parts

PartSubLines

PartPrices

 

Each Group belongs to a Family, each Line to a Group, each SubLine to a Line, and each Part to a SubLine (many-to-many through PartSubLines).

 

There is a 1-to-1 relationship between Parts and PartPrices. They share the PartID field.

 

Here's the thing: There can be more than one price for each part. This allows us to track price history. So there is a unique date with each price.

 

I need to pull every part, including ONLY the most recent date. My thought is to pull a TOP 1 and ORDER BY Date DESC. But I can't figure out how to do that.

 

So far I have been using a Left Outer Join, but that isn't giving me what I need. Here's what I have:

 

SELECT Parts.PartID AS PartID,

  Parts.PartNumber AS PartNumber,

  Parts.Description AS Description,

  PartPrices.Price AS Price

  FROM Parts

  LEFT OUTER JOIN PartPrices ON PartPrices.PartID = Parts.PartID

  PartSublines, Lines, Groups, Families, SubLines

      WHERE Groups.FamilyID = Families.FamilyID

  AND Lines.GroupID = Groups.GroupID

  AND SubLines.LineID = Lines.LineID

  AND SubLines.Obsolete = 0

  AND PartSubLines.SubLineID = SubLines.SubLineID

  AND Parts.PartID = PartSubLines.PartID

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/171815-top-row-in-an-outer-join/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.