I have a query like the following
select *
from (
select *
from callTableFunction(@paramPrev)
.....< a whole load of other joins, wheres , etc >........
) prevValues
full join
(
select *
from callTableFunction(@paramCurr)
.....< a whole load of other joins, wheres , etc >........
) currValues on prevValues.Field1 = currValues.Field1
....<other joins with the same subselect as the above two with different parameters passed in
where ........
group by ....
The following subselect is common to all the subselects in the query bar the @param to the table function.
select *
from callTableFunction(@param)
.....< a whole load of other joins, wheres , etc >........
One option is for me to convert this into a function and call the function, but i dont like this as I may be changing the subselect query quite often for.....or I am wondering if there is an alternative using CTE like
with sometable(@param1) as
(
select *
from callTableFunction(@param)
.....< a whole load of other joins, wheres , etc >........
)
select
sometable(@paramPrev) prevValues
full join sometable(@currPrev) currValues on prevValues.Field1 = currValues.Field1
where ........
group by ....
Is there any syntax like this or technique I can use like this.
This is in SQL Server 2008 R2
Thanks.