SQL Server: Combine several SELECT statements with "WITH" part into a UNION -
SQL Server: Combine several SELECT statements with "WITH" part into a UNION -
i have several of statements (that utilize statement):
with valdiff (select <complexclause1> v1 [mytable] <otherclause1>) select sum(case when v1 < @maxval v1 else @maxval end) valdiff union valdiff (select <complexclausen> v1 [mytable] <otherclausen>) select sum(case when v1 < @maxval v1 else @maxval end) valdiff
i need incorporate them union result returned "in 1 swoop". statements work fine themselves, if add together word "union" between them, showed above, next error:
incorrect syntax near keyword 'union'.incorrect syntax near keyword 'with'. if statement mutual table expression, xmlnamespaces clause or alter tracking context clause, previous statement must terminated semicolon."
what doing wrong?
the span clauses in union
;with valdiff ( whatever ) select ... valdiff ... union select ... valdiff ...
with different cte in each clause (as in case):
;with cte1 ( whatever ) , cte2 ( ) select ... cte1 ... union select ... cte2 ...
sql sql-server select union
Comments
Post a Comment