2016-11-07 79 views
0

我有這個疑問: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 但它只包含簡單的例子。

謝謝。

+0

請問您的查詢給你你想要的結果嗎? –

+0

解決了它。我必須在整個cte –

+0

之後添加一個SELECT * FROM T子句非常好。你能把你的結果作爲答案嗎? –

回答

1

很簡單;忽略了語法。

WITH T([First Name], [Last Name], [Current Salary]) 
AS 
(

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] 
) 
SELECT * FROM T; 
0

這取決於你想要在哪裏休息。通常可以將內聯視圖創建爲CTE,以使查詢更易於閱讀......在這種情況下,我刪除了線視圖並將其作爲CTE。

With C as (
    (
    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) 

SELECT C.[First Name] 
    , C.[Last Name], 
    SUM(Salary.Emp_Salary) AS 'Current Salary' 
FROM C 
INNER JOIN Salary 
    ON C.[Change Year] = Salary.Emp_Salary_Change_Year 
GROUP BY C.[First Name] 
     ,C.[Last Name]; 
0

這取決於你想在哪裏休息。通常可以將內聯視圖創建爲CTE,以使查詢更易於閱讀......在這種情況下,我刪除了線視圖並將其作爲CTE。

With C as (
    (
    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) 

SELECT C.[First Name] 
    , C.[Last Name], 
    SUM(Salary.Emp_Salary) AS 'Current Salary' 
FROM C 
INNER JOIN Salary 
    ON C.[Change Year] = Salary.Emp_Salary_Change_Year 
GROUP BY C.[First Name] 
     ,C.[Last Name]; 

,或者你可以建立在熱膨脹係數...

With C as (
    (
    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), 
CTE2 as ( 
    SELECT C.[First Name] 
     , C.[Last Name], 
     SUM(Salary.Emp_Salary) AS 'Current Salary' 
    FROM C 
    INNER JOIN Salary 
     ON C.[Change Year] = Salary.Emp_Salary_Change_Year 
    GROUP BY C.[First Name] 
      ,C.[Last Name]) 
SELECT * 
FROM CTE2;