APPLY operator was introduced with SQL Server 2005 edition and was great help to join records set with table value function or table value expression.
There are two APPLY operators.
1. CROSS APPLY
2. OUTER APPLY
In simplest terms, CROSS APPLY behave like INNER JOIN and OUTER APPLY behave like LEFT OUTER JOIN.
Let's consider a following example table structure
-- Employee Table
CREATE TABLE Employee (
EmpId INT PRIMARY KEY,
EmployeeName VARCHAR(100),
Age INT,
BranchId INT
);
-- Branch Table
CREATE TABLE Branch (
BranchId INT PRIMARY KEY,
BranchName VARCHAR(100)
);
Here is my test data:
Branch Table
Employee Table
I have a table value function which brings branch id and average age of employees in that branch.
CREATE FUNCTION GetAverageEmployeeAgeByBranch
(
@BranchId INT
)
RETURNS TABLE
AS
RETURN
(
SELECT BranchId, AVG(Age) AS AverageAge
FROM Employee
WHERE BranchId = @BranchId
GROUP BY BranchId
);
I need to show branch id, branch name and average employee age for that branch in my query. First I have used CROSS apply in my query.
SELECT b.BranchId, BranchName, e.AverageAge
From Branch b CROSS APPLY dbo.GetAverageEmployeeAgeByBranch(BranchId) as e
ORDER BY BranchId
Result as follows:
Then I have used OUTER APPLY:
SELECT b.BranchId, BranchName, e.AverageAge
From Branch b OUTER APPLY dbo.GetAverageEmployeeAgeByBranch(BranchId) as e
ORDER BY BranchId
Result as follows:
You can also use APPLY operator with table value expressions (i.e. sub queries).
SELECT b.BranchId, b.BranchName, e.EmployeeName
FROM Branch b CROSS APPLY
(SELECT e.BranchId,
STRING_AGG(EmployeeName, ';') WITHIN GROUP (ORDER BY e.EmployeeName) As EmployeeName
FROM Employee e WHERE e.BranchId = b.BranchId GROUP BY e.BranchId) as e
No comments:
Post a Comment