• I think that one of the methods you could use to make this code a lot easier to work with is to write out just exactly what it is doing in plain English statements. For example:

    CASE WHEN t.dtleasefrom BETWEEN s.begdate AND s.enddate AND t.dtrenewdate IS NULL

    AND ISNULL(p.dtapply, t.dtsigndate) IN ...

    could be written "when the lease date lies within the time period specified by the begin and end date, the renewal date is null and the apply date (or the sign date if apply date is null) is within the same fiscal year as the begin date..."

    One of the advantages of doing that is you should see immediately that to check if a date is within the same year as another date, there is no reason to generate a list of 365 sequential dates and use the IN operator. Just generate the date of the first day of the target fiscal year and the date of the first day of the next fiscal year and use BETWEEN.

    Provide an English explanation of every parameter and column used in your query and I'm sure you will be able to very much simplify the query. Post it here and we will help, if you wish, but just the effort of doing it will allow you to answer many of your own questions.

    Tomm Carr
    --
    Version Normal Form -- http://groups.google.com/group/vrdbms