简体   繁体   English

SQL Server 2012-MAX()函数

[英]SQL Server 2012 - MAX() Function

I am pulling data from 3 separate employee related tables, all joined on SSN. 我正在从3个与员工相关的单独表中提取数据,所有这些表均已加入SSN。 Because some employees have transferred within the company multiple times they have multiple hire/separation dates in the system, so when I pull from that table with the hire dates, it duplicates the row by the number of hire/separation dates in the system. 由于某些员工已在公司内部多次转移,因此他们在系统中具有多个雇用/离职日期,因此当我从该表中提取雇用日期时,它将按系统中的雇用/离职日期数来复制行。 Here's the data sample as it's pulled: 这是提取的数据示例:

SSN         Name        Pay_Date    Hire_Date
123456789   John Doe    5/1/2012    1/1/2001
123456789   John Doe    5/1/2012    2/5/2005
123456789   John Doe    5/1/2012    3/1/2012
123456789   John Doe    5/15/2012   1/1/2001
123456789   John Doe    5/15/2012   2/5/2005
123456789   John Doe    5/15/2012   3/1/2012
123456789   John Doe    5/29/2012   1/1/2001
123456789   John Doe    5/29/2012   2/5/2005
123456789   John Doe    5/29/2012   3/1/2012

The query: 查询:

SELECT

SSN, Name, Pay_Date, Hire_Date

FROM Personnel as PER

LEFT JOIN Payroll as PAY on PER.SSN = PAY.SSN

LEFT JOIN HumanResources as HR on PER.SSN = HR.SSN

ORDER BY Pay_Date(DESC)

To eliminate the rows with hire dates 1/1/2001 and 2/5/2005, I tried using the MAX function as follows with no luck. 为了消除雇用日期为1/1/2001和2/5/2005的行,我尝试使用MAX函数,但没有运气。 I tried using a variety of the examples posted on previous topics related to MAX but nothing is working. 我尝试使用与以前有关MAX的主题发布的各种示例,但没有任何效果。

SELECT

SSN, Name, Pay_Date, MAX(Hire_Date)

FROM Personnel as PER

LEFT JOIN Payroll as PAY on PER.SSN = PAY.SSN

LEFT JOIN HumanResources as HR on PER.SSN = HR.SSN

GROUP BY SSN

ORDER BY Pay_Date(DESC)

Again, I just need the row with the latest hire date. 同样,我只需要有最新雇用日期的那一行。 So the outcome should look like 所以结果应该像

SSN         Name        Pay_Date    Hire_Date
123456789   John Doe    5/1/2012    3/1/2012
123456789   John Doe    5/15/2012   3/1/2012
123456789   John Doe    5/29/2012   3/1/2012
;WITH MyCTE AS
(
    SELECT SSN, 
                   Name, 
                   Pay_Date, 
                   Hire_Date,
                   ROW_NUMBER() OVER(PARTITION BY SSN, Name, Pay_Date ORDER BY Hire_Date DESC) AS rn
    FROM     Personnel as PER
    LEFT JOIN Payroll as PAY on  PER.SSN = PAY.SSN
    LEFT JOIN HumanResources as HR on PER.SSN = HR.SSN
)
SELECT * 
FROM    MyCTE
WHERE rn = 1

SQL Fiddle Demo Powered by Lamak 由Lamak提供支持的 SQL Fiddle演示

Is it the latest Pay date and hire date? 是最新的付款日期和雇用日期吗? if so this should work. 如果是这样,这应该工作。

SELECT

SSN, Name, MAX(Pay_Date), MAX(Hire_Date)

FROM Personnel as PER

LEFT JOIN Payroll as PAY on PER.SSN = PAY.SSN

LEFT JOIN HumanResources as HR on PER.SSN = HR.SSN

GROUP BY SSN, Name

ORDER BY Pay_Date(DESC)

To get the row with the latest hire date: 要获得具有最新雇用日期的行:

SELECT
SSN, Name, Pay_Date, Hire_Date
FROM Personnel as PER
LEFT JOIN Payroll as PAY on PER.SSN = PAY.SSN
LEFT JOIN HumanResources as HR on PER.SSN = HR.SSN
WHERE ROW_NUMBER() OVER (PARTITION BY SSN, ORDER BY Hire_Date DESC)=1
ORDER BY Pay_Date DESC

Aggregate functions in this case are problematic because MAX() data for different columns can come from different rows in your set. 在这种情况下,聚合函数存在问题,因为不同列的MAX()数据可能来自集合中的不同行。

Instead, use window functions to discover what row in each group of SSNs has the highest (most recent) Hire_Date and then select your results from that subset. 而是使用窗口函数查找每组SSN中的哪一行具有最高(最新)的Hire_Date,然后从该子集中选择结果。

SELECT SSN, Name, Pay_Date, Hire_Date
FROM
    (
    SELECT

        SSN
        , Name
        , Pay_Date
        , Hire_Date
        , ROW_NUMBER() over (PARTITION BY SSN ORDER BY Hire_Date DESC) rn
    FROM
        Personnel as PER

        LEFT JOIN Payroll as PAY on PER.SSN = PAY.SSN

        LEFT JOIN HumanResources as HR on PER.SSN = HR.SSN
    ) x

WHERE 
    x.rn = 1

ORDER BY Pay_Date DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM