I am using SQL Server 2008, and I have 3 tables x, yand z. yexists to create a many-to-many relationship between xand z.
x y z
-- -- --
id xid id
zid sort
All of the above fields int.
I want to find the most efficient method (excluding denormalization) for finding zthe highest sortfor any xand return all fields from all three tables.
Sample data:
x: id
--
1
2
y: xid zid
--- ---
1 1
1 2
1 3
2 2
z: id sort
-- ----
1 5
2 10
3 25
The result set should be
xid zid
--- ---
1 3
2 2
Please note that if there is more than one zwith the same highest value sort, then I have only one line left for x.
, , .