Hello Sunshine, I didn't look to deeply into it, but that is one nasty query <g>. I do have a suggestion though, why not have either a table or cursor containing the values that require special treatment ("60","61","62" ...) then do an inner join to that table for the first part of the query, it would replace all those IIFs with straight values, the rest of the records can be appended by another union. This table would have all those special values, as in:
Lcode objcode lic_tot
60 002301 280.00
61 002302 490.00
62 002303 700.00
63 002304 8.40
47 002147 12.00
48 002148 22.00
50 002149 66.00
I know this only covers the first part of the existing union, but you get the idea. It doesn't necessarily fix your problem but it will make the query easier to digest, and hopefully help you find a solution.
Also, have you tried the query with a small set of data? just to make sure it even works as intended.