I have previously written many articles on CTE. One question I get often is how to use multiple CTE in one query or multiple CTE in SELECT statement. Let us see quickly two examples for the same. I had done my best to take simplest examples in this subject.
Option 1 :
/* Method 1 */
;WITH CTE1 AS (SELECT 1 AS Col1),
CTE2 AS (SELECT 2 AS Col2)
SELECT CTE1.Col1,CTE2.Col2
FROM CTE1
CROSS JOIN CTE2
GO
Option 2:
/* Method 2 */
;WITH CTE1 AS (SELECT 1 AS Col1),
CTE2 AS (SELECT COL1+1 AS Col2 FROM CTE1)
SELECT CTE1.Col1,CTE2.Col2
FROM CTE1
CROSS JOIN CTE2
GO
Please note the difference between options. If there is any other option than above mentioned two options, please leave your comment here.
Reference : Pinal Dave (https://blog.sqlauthority.com)
73 Comments. Leave new
Thanx for sharing your knowledge.
I have question. I want to get information from a simple table but optional WHERE clause, but using CTE (for some purpose) How to accomplish my goal but with using string for query ???
Agreed.
@Aasim,
To accomplish CTE with optional parameter, you can use is as this way:
;WITH cte1 AS
( SELECT Col1
FROM Table
WHERE (@Param IS NULL OR Col1 = @Param)
),
cte2 AS (
SELECT Col2
FROM Table
WHERE (@Param IS NULL OR Col2 = @Param)
)
SELECT cte1.Col1,cte2.Col2
FROM cte1
CROSS JOIN cte2
GO
Here, if @param has value, then it will filter result in CTE.
Let me know if it helps you.
Thanks,
Tejas
Well Explanation
Well englishes
well well welll
@aasim
WITH CTE(String) AS (SELECT ‘a string’)
SELECT String FROM CTE;
” I had done my best to take simplest examples in this subject.”
These examples would be a lot easier to follow if you took an extra minute to use real column and table names. Practical real world scenarios are always the best examples.
Thanks
@Wes
“Practical real world scenarios are always the best examples.”
I disagree. For me, the best examples remove all unnecessary things and focus just on the issue. Only then should it be compared to reality.
But, to each their own.
Hello Sir,
I need your help for getting result from query.
I have three tables say ProductMaster, Genre and Product_Genre.
Records contained in ProductMaster table are like follows-
Product_ID ProductName Price
1 XYZ 20.10
2 ABC 11.35
3 PQR 05.33
Records contained in Genre table are like follows-
Genre_ID GenreName
101 Horror
102 Romantic
103 Suspense
Records contained in Product_Genre table are like follows-
Genre_ID ProductID
101 1
102 1
101 2
101 3
103 3
Now I want result like as follows-
Product_ID ProductName GenreName Price
1 XYZ Horror | Romantic 20.10
2 ABC Horror 11.35
3 PQR Horror | Suspense 05.33
Sir so how could I get this result please help me.
Thanks.
Regards,
Mandar Kavishwar
Great articale I enjoyed . :)
Hi pinal,
Very useful article… i got this link when i was strucked with 2 CTE , it really helped me…
Thank you
regards,
Divya
What is the difference between CTE and table variables?
please give me answer..
You could say that CTE is like a ad-hoc view. When you call it it produces output from the data that is stored in the DB at the time of the call. Like you would select data from any normal table or view. One huge difference to normal table is that you can’t update or insert data to CTE.
Table variable on the other hand is a variable. You can store table data to it and select data from it multiple times and the data does not change even when the data in the DB changes. You can update or insert data to table variable also. And you can create indexes to table variable, something you can’t do with CTE.
Hello sir,
Great article; precisely the issue I will need your expert help on.
I have several columns that require Current Value (‘Reporting Week’) and YTD counts.
I felt like the best choice at solving this problem is to use CTE.
However, the issue I am running into is that after I get a sub total, how do I continue adding more columns?
For instance, I select the following columns:
StructuralFires, Non_StructuralFires, Non_Fire_Emergencies,
Fire_Alarms, Hazardous_MaterialsRespIncids and I get sub total called ‘Total Fire Incidents’ as shown below:
FIeldname Reporting Week YTD
————————————————————
StructuralFires 1 1
Non-StructuralFires 1 1
Non-Emergencies 0 4
Emergencies 1 3
Hazards 9 31
Total Fire Incidents 12 40
After this total, I want to continue selecting more columns.
Then eventually I get GrandTotal.
How do I do this?
This is the code I am working with so far. Please help me with your expertise.
Declare @StartDate DateTime;
Declare @EndDate DateTime;
Declare @ReportYear Int;
set @StartDate = ‘9/26/2009′;
set @EndDate = ’10/2/2009’;
— Determine the year you are working with.
Set @ReportYear =
(
Select YEAR(StartDate)
From FireReportsData
Where (startDate = @StartDate and EndDate = @EndDate)
);
— Build the CTEs to hold your unpivoted data.
With WeeklyReport (IncidentType, [Reporting Week])
As
(
Select IncidentType, [Reporting Week]
From
(
Select ReportQID, StructuralFires, Non_StructuralFires, Non_Fire_Emergencies,
Fire_Alarms, Hazardous_MaterialsRespIncids
From FireReportsData
Where (startDate = @StartDate and EndDate = @EndDate)
) As FireData
UnPivot
(
[Reporting Week] For IncidentType In ([StructuralFires], [Non_StructuralFires],
[Non_Fire_Emergencies], [Fire_Alarms], [Hazardous_MaterialsRespIncids])
) As FireDataUnpivot
),
YtdReport (IncidentType, YTD)
As
(
Select IncidentType, [Reporting Week]
From
(
Select SUM(StructuralFires) As [StructuralFires],
SUM(Non_StructuralFires) As [Non_StructuralFires],
SUM(Non_Fire_Emergencies) As [Non_Fire_Emergencies],
SUM(Fire_Alarms) As [Fire_Alarms],
SUM(Hazardous_MaterialsRespIncids) As [Hazardous_MaterialsRespIncids]
From FireReportsData
Where (YEAR(StartDate) = @ReportYear)
) As FireData
UnPivot
(
[Reporting Week] For IncidentType In ([StructuralFires], [Non_StructuralFires],
[Non_Fire_Emergencies], [Fire_Alarms], [Hazardous_MaterialsRespIncids])
) As FireDataUnpivot
)
— Join the weekly and monthly results based off of incident type to produce
— your result set.
Select W.IncidentType, W.[Reporting Week], Ytd.YTD
From WeeklyReport As W
Inner Join YtdReport As Ytd On W.IncidentType = Ytd.IncidentType
Union All
— Create a “totals” record.
Select ‘Total Fire Incidents’ As [IncidentType], SUM(W.[Reporting Week]) As [Reporting Week],
SUM(Ytd.YTD) As [YTD]
From WeeklyReport As W
Inner Join YtdReport As Ytd On W.IncidentType = Ytd.IncidentType;
Go
i have a doubt!
Can we use Single CTE for multiple select statements?
Hi i have a query like this
With CTE1 AS
(
)
,CTE2 AS
(
)
If(@inputId = 0)
SELECT * from CTE1
else
SELECT * from CTE2
.Ca nanyone help me out how to solve this?
if you want to use order by in both select query then it will not work. then for this condition what should be done. please reply asap
use temp table.
thanks you pinal. you are great for us. thanks a lot . why don’t you start a some coaching
Couldn’t have said it better myself Wonderful information Please keep blogging!!
Hi Krishnan,
After CTE there should be a SELECT and DML statements only. You can use SELECT/INSERT/UPDATE/DELETE only.
If you need to have condition then you have to use alternate way like Table variable.
Tejas
SQLYoga.com
Hi Pinal,
Can we run cte queries on our production DB.
Please suggest me.
Thanks
Rahul
@rahul.bhargava
Yes. CTEs are part of normal queries.
Hi i have a table having
IDNo code
1234 1
3456 1
2345 2
1456 2
1234 2
2345 1
1456 1
2806 2
here for duplicate id i need to select id no having code as 1
The output should be
idno code
1234 1
3456 1
2345 1
1456 1
2806 2
if i do that it is effecting other idno i heard it can be done with cte can anyone help me in solving above problem
@Raj
How’s this?
WITH
Data(IDNo, code)
AS
(
SELECT 1234, 1 UNION ALL
SELECT 3456, 1 UNION ALL
SELECT 2345, 2 UNION ALL
SELECT 1456, 2 UNION ALL
SELECT 1234, 2 UNION ALL
SELECT 2345, 1 UNION ALL
SELECT 1456, 1 UNION ALL
SELECT 2806, 2
)
SELECT
IDNo,
MIN(code)
FROM
Data
GROUP BY
IDNo;
Hi Raj,
You can use this solution, if you are using SQL 2005 and above.
I have used ROW_Number to identify if it is duplicate or not and get output as you want.
DECLARE @table TABLE(IDNO INT, Code INT)
INSERT INTO @table(IDNo, Code)
SELECT 1234, 1
UNION
SELECT 3456,1
UNION
SELECT 2345, 2
UNION
SELECT 1456, 2
UNION
SELECT 1234, 2
UNION
SELECT 2345, 1
UNION
SELECT 1456, 1
UNION
SELECT 2806, 2
;with cte as (
SELECT ROW_NUMBER() OVER(PARTITION BY IDNo ORDER BY Code) AS RowID,
IDNO,
Code
FROM @table
)
SELECT IDNO,
Code
FROM cte
WHERE RowID = 1
Thanks,
Tejas
SQLYoga.com
Hi Brian, Tejas Shah
Thanks for reply. i solved the issue in different way since i have 2million records..
with ctetbl1
as(
select affinityNumber from tbl
group by ID having COUNT(*)>1),
cteTemp
as(
select *
from tbl where AffinityNumber not in(select id from ctetbl1 where ID =’2′)
select * from cte temp
Thanks
Raj
CTEs(Common Table Expressions) are one of the beautiful and the most powerful feature of SQL Server and are boon to SQL Server developers. These not only simplifies most of the complex operations in T-SQL but also do a lot more than one can imagine.
h ttp://www.sqllion.com/2010/08/common-table-expressions-cte/
I have two tables tblParent and tblmembers
tblParent
———
parentid(PK), parentname, address, age
tblMembers
———-
membername, age, sex, parentid(FK)
——————————-
My report should show like this:
——————————–
100 Edwin QRY 50
1 Sushi 45 F
2 Craig 26 M
3 Mira 23 F
101 John TVM 30
1 Sara 26 F
2 Sam 3 M
____________________________________________
Thanks in advance…. :)
You need to post the sample data with the logic