我有這個疑問:CTE對於複雜查詢
SELECT C.[First Name],
C.[Last Name],
SUM(Salary.Emp_Salary) AS 'Current Salary'
FROM
(
SELECT
Emp_First_Name AS "First Name",
Emp_Last_Name AS "Last Name",
MAX(Emp_Salary_Change_Year) AS "Change Year"
FROM Employee_Details AS e
INNER JOIN Country AS co ON e.Emp_Country_Id = co.Country_Id
INNER JOIN State AS s ON e.Emp_State_Id = s.State_Id
INNER JOIN Designation AS d ON e.Desig_Id = d.Desig_Id
INNER JOIN Salary AS sa ON e.Emp_Id = sa.Emp_Id
WHERE Emp_Active = 1
GROUP BY Emp_First_Name, Emp_Last_Name,
Emp_Middle_Name, Country_Name, Desig_Name) AS C
INNER JOIN Salary ON C.[Change Year] = Salary.Emp_Salary_Change_Year
group by C.[First Name],
C.[Last Name];
返回:
First Name Last Name Current Salary
Chen Chen 76000.00
Sahi King 156000.00
Venessa Katarina 185000.00
如何使用CTE此查詢? 我知道使用WITH子句,但我無法使它工作,因爲查詢很複雜。我使用這個參考:http://www.codeproject.com/Articles/265371/Common-Table-Expressions-CTE-in-SQL-SERVER 但它只包含簡單的例子。
謝謝。
請問您的查詢給你你想要的結果嗎? –
解決了它。我必須在整個cte –
之後添加一個SELECT * FROM T子句非常好。你能把你的結果作爲答案嗎? –