Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need to filter large CTE in Join or where clause Expand / Collapse
Author
Message
Posted Wednesday, October 24, 2012 4:32 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 10, 2014 12:20 PM
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







Post #1376721
Posted Wednesday, October 24, 2012 4:52 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Sunday, July 20, 2014 7:16 AM
Points: 945, Visits: 1,769
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

Jeremy Oursler
Post #1376724
Posted Wednesday, October 24, 2012 4:56 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, February 27, 2013 6:54 AM
Points: 40, Visits: 168
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
Post #1376726
Posted Wednesday, October 24, 2012 5:11 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 10, 2014 12:20 PM
Points: 33, Visits: 173
Awesome SSC Rookie... this works Not sure if you can mark your reply as the answer or not, but it works

Thanks
Post #1376730
Posted Thursday, October 25, 2012 2:32 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:35 PM
Points: 4,013, Visits: 6,087
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? -- Stephen Stills
Post #1377291
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse