Using SQL Server 2008, I have a table abc and xyz
Abc
abc_id | xyz_id1 | xyz_id2
---------------------------
1 | foo123 | foo125
2 | foo127 | foo129
Xyz table
xyzid | abc_id | location | sequence_id
------------------------------------------
foo123 | 1 | park | 1
foo124 | 1 mall | 2
foo125 | 1 | park | 3
foo127 | 2 | restaurant | 1
foo128 | 2 | lake | 2
... -- several xyz records for order 2
foo130 | 2 | mall | 5
I need to get all abc_id where its location xyz_id1 is equal to the address xyz_id2. (park == park). xyz_id1 and xyz_id2 will always be the minimum and maximum sequence number in the xyz table, so probably the query will use max (sequence_id).
In this example, it will return "1". (this will return a lot of records, not just a scalar value).
There is some business logic in my code that I would prefer not to use. Can anyone help? I'm sure I need a subquery or a temporary table?
James source
share