-1

我想从 sql server 2088 R2 查询。但它是重复值。

我需要它不是重复值​​​。

代码:

Select es.EmpCode,es.Eff,es.Year,MAX(es.Month)
From SP_EffEmpAnalysis es
Where es.Year = '2012'
Group By es.EmpCode,es.Eff,es.Year
Order By es.EmpCode,MAX(es.Month) desc

输出

EmpCode | Eff | Year | Month
1410013 | 0 | 2012 |    11
1450021 | 0.952700018882751 |   2012 |  8
1460037 | 0.653999984264374 |   2012 |  9
1460037 | 0.809899985790253 |   2012 |  8
1460037 | 0.863600015640259 |   2012 |  7
1460047 | 0.796299993991852 |   2012 |  7
1480013 | 0             | 2012 |    11
1480080 | 0.91839998960495 |    2012 |  12
1480080 | 0.982299983501434 |   2012 |  11
1480080 | 1.08259999752045 |    2012 |  10
1480080 | 0.989700019359589 |   2012 |  9

我需要输出

EmpCode | Eff | Year | Month
1410013 | 0 | 2012 |    11
1450021 | 0.952700018882751 |   2012 |  8
1460037 | 0.653999984264374 |   2012 |  9
1460047 | 0.796299993991852 |   2012 |  7
1480013 | 0             | 2012 |    11
1480080 | 0.91839998960495 |    2012 |  12
4

2 回答 2

1

我认为您正在寻找的是上个月每位员工的价值。您可以使用 row-number 函数获得它:

SELECT EmpCdoe, Eff, Year, Month
FROM(
SELECT *,ROW_NUMBER()OVER(PARTITION BY EmpCode ORDER BY Year DESC, Month DESC) AS frn
WHERE Year = '2012'
)X
WHERE frn=1
ORDER BY EmpCode;
于 2013-01-08T03:10:19.337 回答
0

塞巴斯蒂安打字更快,但我认为这更完整:

select EmpCode, Eff, Year, Month
  from (
    select es.EmpCode, es.Eff, es.Year, es.Month,
      Row_Number() over ( partition by es.EmpCode order by es.Year desc, es.Month desc ) as RN
      from SP_EffEmpAnalysis es
      where es.Year = '2012' ) as Placeholder
  where RN = 1
  order By es.EmpCode
于 2013-01-08T03:15:26.153 回答