• I feel compelled to post a warning against using such a technique on tables with production-scale volumes. It can lead to poor execution plans.

    Given that you don't want to duplicate the large blocks of code embodied in your ctes, you might want to performance test the following alternative logic.

    with cte as (-- this is the last of your big set of ctes)

    select *

    from cte

    where @egroups = 'False'

    union all

    select * from cte

    WHERE exists(select 1 from @fips EG where fips = fips_state_county)

    and isnull(@egroups,'True') <> 'False'

    There may be a more appropriate place to place the existence test, but that would require seeing your actual code for the ctes in their entirety.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills