Need to filter large CTE in Join or where clause

  • So I have a fairly large CTE in a stored procedure. I want to filter my results based on an parameter called @Egroups. if the value of the parameter is true I only want EGroups. If false, I want all groups. Egroups are kept in a table. So the CTE should use the table to filter if @Egroups is True else do not use the table.

    I thought of something like this......

    if @Egroups = 'True'

    Begin

    select Groups from companyGroups CG join Egroups EG on CG.id = EG.id

    End

    else

    Begin

    select Groups from companyGroups

    End

    --OR.......

    if @Egroups = 'True'

    Begin

    select Groups from companyGroups

    where id in (select idmanager from Egroups)

    End

    else

    Begin

    select Groups from companyGroups

    End

    But my CTE is about 200 lines long, not a simple select * from companyGroups, so I would be duplicateing hundreds of lines of code

    Does anyone know how to add a table to a join based on an input... or add an additioanl where clause based on an input? Maybe some kind of case logic.

    Any suggestions appreciated.

    Thanks

  • You have something close to solvable. can you post DDL, Sample data, and the desired output for your tables stripped down to resemble the problem. That will help allot on this problem. If you are not sure what im asking about the first link in my signature has all the details.

    since you are asking a specific tuning question more information will be helpful.


    For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden[/url] for the best way to ask your question.

    For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]

    Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
    Jeff Moden's Cross tab and Pivots Part 1[/url]
    Jeff Moden's Cross tab and Pivots Part 2[/url]

  • Try something like this:

    select Groups from companyGroups CG

    where @Egroups = 'False' or

    exists(select 1 from Egroups EG where CG.id = EG.id)

    Hope it's useful.

  • Awesome SSC Rookie... this works :w00t: Not sure if you can mark your reply as the answer or not, but it works

    Thanks

  • 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

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply