我想从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
答案 0 :(得分:1)
我认为你要找的是每个员工上个月的价值。你可以使用行号函数来获得它:
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;
答案 1 :(得分: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