Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need to filter large CTE in Join or where clause


Need to filter large CTE in Join or where clause

Author
Message
computer.mike
computer.mike
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 173
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
CapnHector
CapnHector
SSC Eights!
SSC Eights! (917 reputation)SSC Eights! (917 reputation)SSC Eights! (917 reputation)SSC Eights! (917 reputation)SSC Eights! (917 reputation)SSC Eights! (917 reputation)SSC Eights! (917 reputation)SSC Eights! (917 reputation)

Group: General Forum Members
Points: 917 Visits: 1789
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 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

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2
imex
imex
Valued Member
Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)

Group: General Forum Members
Points: 69 Visits: 199
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.

http://www.imoveisemexposicao.com.br
computer.mike
computer.mike
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 173
Awesome SSC Rookie... this works w00t Not sure if you can mark your reply as the answer or not, but it works

Thanks
The Dixie Flatline
The Dixie Flatline
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3398 Visits: 6899
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search