-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql-cte-examples.sql
162 lines (137 loc) · 4.23 KB
/
sql-cte-examples.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
SELECT * FROM StudentTotalMarks
WITH MarksCTE(StudentName,Percentage,TotalObtained) AS
( SELECT StudentName,
(CAST(MarksObtained AS float)/ CAST(TotalMarks AS float)*100) Percentage,
MarksObtained
FROM StudentTotalMarks
)
SELECT * FROM MarksCTE
SELECT * FROM EmployeeDuplicate
WITH DuplicateCTE(EmpID,EmpName,Department,RowID) AS
(
SELECT EmpID,
EmpFirstname+''+EmpLastName as EmpName,
Department,
ROW_NUMBER()OVER(PARTITION BY EmpFirstname,EmpLastName,
Department ORDER BY EmpID)AS RowID
FROM EmployeeDuplicate
)
DELETE FROM DuplicateCTE WHERE RowID>1
Select * from DuplicateCTE
The query below creates a temporary result set as :
SELECT Eid,Ename,Dept, ROW_NUMBER()OVER(PARTITION BY Ename,Dept ORDER BY EID)AS RowID
FROM EMP
Select * from EmployeeDetails
Select * from EmpSalary
Select * from EmployeeProject
WITH EmployeeCTE(EmpID,EmpName,Department,DepartID,EmpSalary,Count)
AS(
SELECT E.EmpID,
S.EmpFullName,
E.Department,
E.DepartID,
S.EmpSalary,
1
FROM EmployeeDetails E inner join EmpSalary S
on E.EmpID = S.EmpID
UNION ALL
SELECT S.EmpID,S.EmpFullName,E.Department,E.DepartID,S.EmpSalary,c.count +1
from EmployeeDetails E inner join EmpSalary S
on S.DepartID = E.DepartID
and S.EmpID = E.EmpID
inner join EmployeeCTE C
on C.EmpID = S.EmpID
)
Select EmpID,EmpName,Department,DepartID,EmpSalary,COUNT
from EmployeeCTE C where Department in ('IT','Accounts') ;
------------------------------------------------------------------
WITH EmployeeCTE(EmpID,EmpName,DepartID,EmpSalary)
AS(
SELECT S.EmpID,
S.EmpFullName,
S.DepartID,
S.EmpSalary
FROM EmpSalary S
UNION ALL
SELECT S.EmpID,S.EmpFullName,E.DepartID,S.EmpSalary
FROM EmployeeDetails E INNER JOIN EmpSalary S
ON S.DepartID = E.DepartID
INNER JOIN EmployeeCTE C ON E.EmpID = C.EmpID
)
SELECT EmpID,EmpName,DepartID,EmpSalary
FROM EmployeeCTE ;
Select * from EmpSalary
SELECT S.EmpID,
S.EmpFullName,
S.DepartID,
S.EmpSalary
FROM EmpSalary S
UNION ALL
SELECT S.EmpID,S.EmpFullName,E.DepartID,S.EmpSalary
from EmployeeDetails E inner join EmpSalary S
on S.DepartID = E.DepartID
and S.EmpID = E.EmpID
SELECT * FROM EmployeeDetails
Alter table EmployeeDetails
add ManagerID int
Insert into EmployeeDetails
values (1006,'Chris','Berg','IT',2,10)
Update EmployeeDetails
set ManagerID = 1005
where EmpID in (1003,1004) 2
-----------------------------------------
WITH
EmployeeCTE (EmpID, FirstName, LastName, ManagerID,Department, EmpLevel)
AS
(
SELECT EmpID, EmpFirstName, EmpLastName, ManagerID, Department, 1
FROM EmployeeDetails
WHERE ManagerID IS NULL
UNION ALL
SELECT E.EmpID,E.EmpFirstName,E.EmpLastName,E.ManagerID, E.Department,
C.EmpLevel + 1
FROM EmployeeDetails E
INNER JOIN EmployeeCTE C
ON E.ManagerID = C.EmpID
)
SELECT
FirstName ,LastName,EmpLevel,Department,
(SELECT EmpFirstName + ' ' + EmpLastName FROM EmployeeDetails
WHERE EmpID = EmployeeCTE.ManagerID) AS Manager
FROM EmployeeCTE
ORDER BY EmpLevel, ManagerID
GO
USE AdventureWorks2012
GO
WITH VendorCTE AS
(SELECT VendorID, SUM(TotalDue) AS TotalDue
FROM Purchasing.PurchaseOrderHeader PH
JOIN Purchasing.PurchaseOrderDetail PD
ON PH.PurchaseOrderID = PD.PurchaseOrderID
GROUP BY VendorID)
SELECT V.AccountNumber, V.Name, VC.SumTotalDue
FROM VendorCTE VC
JOIN Purchasing.Vendor V
ON V.BusinessEntityID = VC.VendorID
ORDER BY TotalDue DESC
GO
USE Northwind
GO
WITH OrderCTE
( CustomerID, CompanyName, ContactName, City, OrderID, OrderDate, ShippedDate)
AS
(
SELECT C.CustomerID, CompanyName, ContactName, City, OrderID, OrderDate, ShippedDate
FROM Orders O JOIN Customers C ON O.CustomerID = C.CustomerID
WHERE C.CustomerID = 'ALFKI'
),
OrderDetailsCTE
( OrderID, ProductID, ProductName, UnitPrice, Quantity)
AS
(
SELECT OD.OrderID, P.ProductID, ProductName, OD.UnitPrice, Quantity
FROM Products P JOIN [Order Details] OD ON P.ProductID = OD.ProductID
)
SELECT * FROM OrderCTE O
JOIN OrderDetailsCTE OD ON O.OrderID = OD.OrderID
GO