I have two tables; Parameters and Concessions. The parameters table has a foreign key field to concessions that is populated only if a concession exists. If a concession is put in place, a duplicate record in Parameters is created with the duplicate having a foreign key entry to concessions. When the concession expires, the duplicate is removed. For example:
Param_key Param_value test_fk Param_Version concession_fk
-----------------------------------------------------------
0 6.0 1 1
1 2.5 2 1
2 3.25 2 2
3 7.7 3 1
4 9.31 4 1
5 9.32 4 2
6 9.33 4 2 17
7 8.0 5 1
8 8.022 5 2
9 8.01 5 3
I have a derived table query that returns the data in test_fk ascending order, taking the highest value of Param_Version of each test_fk;
What I need to try and do is, if a concession_fk entry exists choose that record over the same record without a concession_fk entry like this:
Param_key Param_value test_fk Param_Version concession_fk
-----------------------------------------------------------
0 6.0 1 1
2 3.25 2 2
3 7.7 3 1
6 9.33 4 2 17
9 8.01 5 3
All my attempts seem to either return both test 4 version 2 records or none at all. Any ideas will be much appreciated.
Thanks
Param_key Param_value test_fk Param_Version concession_fk
-----------------------------------------------------------
0 6.0 1 1
1 2.5 2 1
2 3.25 2 2
3 7.7 3 1
4 9.31 4 1
5 9.32 4 2
6 9.33 4 2 17
7 8.0 5 1
8 8.022 5 2
9 8.01 5 3
I have a derived table query that returns the data in test_fk ascending order, taking the highest value of Param_Version of each test_fk;
Code:
SELECT Param_value, test_fk, Param_Version FROM Parameters INNER JOIN (SELECT test_fk, MAX(Param_Version) AS latest_ver FROM Parameters GROUP BY test_fk) MaxParam ON Parameters.test_fk = MaxParam.test_fk AND Parameters.Param_version = MaxParam.latest_ver;
What I need to try and do is, if a concession_fk entry exists choose that record over the same record without a concession_fk entry like this:
Param_key Param_value test_fk Param_Version concession_fk
-----------------------------------------------------------
0 6.0 1 1
2 3.25 2 2
3 7.7 3 1
6 9.33 4 2 17
9 8.01 5 3
All my attempts seem to either return both test 4 version 2 records or none at all. Any ideas will be much appreciated.
Thanks