CTE用于复杂查询

时间:2016-11-07 18:22:00

标签: sql sql-server tsql common-table-expression

我有这个问题:

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 但它只包含简单的例子。

感谢。

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

这取决于你想要休息的地方。通常可以将内联视图创建为CTE以使查询更易于阅读...在这种情况下,我删除了您的线条视图并使其成为CTE。

    $query = "SELECT [Distinct Nazwa] FROM vDane ";
    $query = "SELECT `Distinct Nazwa` FROM vDane ";

答案 2 :(得分: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];

或者你可以建立在ctes ...

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;