I want to do this :
WITH temp (f1, f2, ...)
BEGIN
SELECT * FROM temp WHERE <condition 1>
SELECT * FROM temp WHERE <condition 2>
END
but SQL Server raise error... is there any solution to do this ?
I want to do this :
WITH temp (f1, f2, ...)
BEGIN
SELECT * FROM temp WHERE <condition 1>
SELECT * FROM temp WHERE <condition 2>
END
but SQL Server raise error... is there any solution to do this ?
You can define multiple CTEs separated with a comma ,
something like this.....
;WITH temp1 (f1, f2, ...) AS
(
SELECT * FROM temp WHERE <condition 1>
),
Temp2 (f1, f2, ...) AS
(
SELECT * FROM temp WHERE <condition 2>
)
SELECT ......
Or if you want combine results returned from both queries you can do this in One cte something like this....
;WITH temp1 (f1, f2, ...) AS
(
SELECT * FROM temp WHERE <condition 1>
UNION ALL
SELECT * FROM temp WHERE <condition 2>
)
SELECT ......
you can use a UNION ALL
to merge both datasets
WITH temp AS (select * from mytable)
SELECT * FROM temp WHERE <condition 1>
UNION ALL
SELECT * FROM temp WHERE <condition 2>