SQL Query to pull two pieces of data from a table with the same link
Posted on 2014-10-13
On a scale of 1 to 10 (1 being completely new and 10 being an expert), when it comes to T-SQL queries I'm probably a 2 or 3. I can do basic selects and updates, linking tables, and other primitive functions like LEN, etc.
The problem I'm running into is I'm trying to link to a table that has TWO values I need. "StartingWeight" and "FinishWeight" of a product. The link is "OwnerIdent", which links to the actual workorder table "ident".
So for instance:
OwnerIdent Attribute Value
001 StartWeight 1500
001 FinishWeight 1520
002 StartWeight 1592
002 FinishWeight 1692
003 StartWeight 1792
003 FinishWeight 1805
004 StartWeight 85
004 FinishWeight 92
I can link the two tables on a query to dbo.workorders like so:
left outer join dbo.weights on dbo.workorders.ident = dbo.weights.Ownerident
but how do I get both the startweight and finishweight into two different columns on the query? For instance, I want to have:
Workorder StartWeight FinishWeight
001 1500 1520
002 1592 1692
and so forth.
The only thing I can figure out how to do is:
left outer join dbo.weights on dbo.workorders.ident = dbo.weights.Ownerident and Attribute = 'StartWeight'
...that gets me only the start weight, but I want both
Hope that makes sense
Appreciate any help :)