Subject Re: [firebird-support] Filtering records that fall into a date range recorded in the settings table
Author Doug Chamberlin
> JOIN USETTINGS USETTINGS
> ON tRMTestCard.TDT BETWEEN (SELECT US1.DVALUE FROM USETTINGS US1 JOIN USETTINGS US2 ON US2.IID =
US1.IPID WHERE US2.CKEY = 'Current Year' AND US1.CKEY = 'From')
> AND (SELECT US3.DVALUE FROM USETTINGS US3 JOIN USETTINGS US4 ON US4.IID = US3.IPID WHERE US4.CKEY = 'Current Year' AND US3.CKEY = 'To')

I would change this to

WHERE tRMTestCard.TDT BETWEEN
(SELECT US1.DVALUE FROM USETTINGS US1 JOIN USETTINGS US2 ON US2.IID
= US1.IPID WHERE US2.CKEY = 'Current Year' AND US1.CKEY = 'From')
AND
(SELECT US3.DVALUE FROM USETTINGS US3 JOIN USETTINGS US4 ON US4.IID
= US3.IPID WHERE US4.CKEY = 'Current Year' AND US3.CKEY = 'To')

No need to join to the USETTINGS table. Just select the two date values
from it.

Also, you have not shared the structure of the USETTINGS table so I
cannot say for certain but it strikes me as overly complex to make it a
self-joining table. I would just have some ID field(s) to select on and
a FROMDATE and TODATE field in each record.