On Jul 16, 3:02 pm, "Aaron Bertrand [SQL Server MVP]"
<ten....@dnartreb.noraa> wrote:
> How are the CTEs joined? And if the where clause *could* be applied within
> the CTE, why isn't it applied there?
[quoted text clipped - 20 lines]
> > ctes, and based on the performance, I can tell it is evaluating 1 of
> > the CTEs before the WHERE clause is applied to it.
The where clause can't be applied there because it is dynamic. If I
take a specific instance though and move it in the CTE, it is much
faster (20 sec vs 230 sec).
The query is very complicated- 3 CTEs and a total of around 40 tables
(some repeated in diff ctes). Too make a very long story short, the
only reason I am using this CTE in issue is that the user can select
criteria dynamically. This consists of 14 different fields. I cannot
apply those directly to the query because you cannot use aliases in
the where clause. I am therefore wrapping it in a CTE and then
applying this dynamic where clause.
Here's an example: I get in something that could be like "WHERE foo
BETWEEN '01/01/2007' AND '02/01/2007'". My select looks like this
(obviously simplified):
SELECT CASE WHEN xxxxx THEN blah WHEN yyyyy THEN fdfdfd WHEN ....
END as foo
FROM tttt
I am wrapping this select in the CTE so I can just apply the where
clause as is.
Aaron Bertrand [SQL Server MVP] - 16 Jul 2008 21:40 GMT
> criteria dynamically. This consists of 14 different fields. I cannot
> apply those directly to the query because you cannot use aliases in
> the where clause.
But you can use them in a subquery / derived table. This is a common
workaround to not having to repeat calculations, e.g.
SELECT fullname
FROM (SELECT fullname = FirstName + ' ' + LastName FROM Customers) x
GROUP BY fullname;
A
Mike C - 16 Jul 2008 22:10 GMT
On Jul 16, 3:40 pm, "Aaron Bertrand [SQL Server MVP]"
<ten....@dnartreb.noraa> wrote:
> > criteria dynamically. This consists of 14 different fields. I cannot
> > apply those directly to the query because you cannot use aliases in
[quoted text clipped - 8 lines]
>
> A
Well, isn't a derived table and CTE essentially the same thing? To me
it is.
Keep in mind I have at least 80 fields, about 12 of which are CASE
statements.
TheSQLGuru - 16 Jul 2008 21:44 GMT
Since you are doing dynamic sql, are you SURE you can't do the entire
excutable statement in dynamic sql without CTEs (or perhaps with them but
collapse the wheres/sargs)??

Signature
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net
On Jul 16, 3:02 pm, "Aaron Bertrand [SQL Server MVP]"
<ten....@dnartreb.noraa> wrote:
> How are the CTEs joined? And if the where clause *could* be applied within
> the CTE, why isn't it applied there?
[quoted text clipped - 21 lines]
> > ctes, and based on the performance, I can tell it is evaluating 1 of
> > the CTEs before the WHERE clause is applied to it.
The where clause can't be applied there because it is dynamic. If I
take a specific instance though and move it in the CTE, it is much
faster (20 sec vs 230 sec).
The query is very complicated- 3 CTEs and a total of around 40 tables
(some repeated in diff ctes). Too make a very long story short, the
only reason I am using this CTE in issue is that the user can select
criteria dynamically. This consists of 14 different fields. I cannot
apply those directly to the query because you cannot use aliases in
the where clause. I am therefore wrapping it in a CTE and then
applying this dynamic where clause.
Here's an example: I get in something that could be like "WHERE foo
BETWEEN '01/01/2007' AND '02/01/2007'". My select looks like this
(obviously simplified):
SELECT CASE WHEN xxxxx THEN blah WHEN yyyyy THEN fdfdfd WHEN ....
END as foo
FROM tttt
I am wrapping this select in the CTE so I can just apply the where
clause as is.
Mike C - 16 Jul 2008 22:12 GMT
> Since you are doing dynamic sql, are you SURE you can't do the entire
> excutable statement in dynamic sql without CTEs (or perhaps with them but
[quoted text clipped - 57 lines]
> I am wrapping this select in the CTE so I can just apply the where
> clause as is.
I could use a view or derived table, but don't see how that is going
to help performance.....they are all functionally the same thing.
Mike C - 17 Jul 2008 15:24 GMT
> > Since you are doing dynamic sql, are you SURE you can't do the entire
> > excutable statement in dynamic sql without CTEs (or perhaps with them but
[quoted text clipped - 63 lines]
> I could use a view or derived table, but don't see how that is going
> to help performance.....they are all functionally the same thing.
Problem solved---not exactly elegant, but it works. I do a replace on
the WHERE clause passed in and replace it with whatever is in the
select list. Basically, something like REPLACE('WHERE
authorid=11','authorid',' CASE WHEN xxxx THEN yyyy WHEN fdsfd then
sss').