我有一个视图,它返回具有字段性能的重复行,可以是1,2,3或4.我只需要选择具有最大性能的行。这该怎么做 ?试过这个:
查看:
结果需要如下:
所以基本上对于每个员工编号组,我需要获取最大日历值和相应的性能值
答案 0 :(得分:3)
你可以表达
the employee record with the highest performance
为:
there is no record (for this employee) with a higher performance
这就是:
SELECT * FROM employee e
WHERE NOT EXISTS (
SELECT *
FROM employee nx
WHERE nx.employee_nr = e.employee_number
AND nx.performance > e.performance
);
答案 1 :(得分:0)
您需要执行两个查询:对于任何给定的max calender
,请阅读employee group
,然后选择具有相同值的行是日历和组。
Select vm."Employee Number" as eGroup, max(vm.Calender) as Calender From view1 vm
那部分是显而易见的。问题是如何将其作为
的标准注入 Select vd."Employee Number", vd.Calender, vd.Performance From view1 vd where ... ?
由于聚合函数(如max
)给出了单数结果,因此您可以使用JOIN而不会产生笛卡尔集生成的风险(当尝试来自两个select
的所有行组合时,NxM)< / p>
Select vd."Employee Number", vd.Calender, vd.Performance From view1 vd
Join (Select "Employee Number" as eGroup, max(vm.Calender) as maxCal From view1) as vm
On (vd."Employee Number" = vm.eGroup) and (vd.Calender = vm.maxCal)
注意,如果它们在表格中的方式,这仍然可以生成具有相同Employee Number
和Calender
的多个行。除非你在这两列上都有一个独特的索引。
答案 2 :(得分:0)
感谢大家的投入。我尝试了一种稍微不同的方法。
步骤1:由于我没有每行的唯一标识符,我通过连接Employee_Number和Calender列创建了一个。 它变得更容易,我们可以使用以下:
SELECT DISTINCT
T1.EMPLOYEE_NUMBER, T1.Performance, T1.Calender
FROM dbo.view1 AS T1 LEFT OUTER JOIN dbo.view1 AS T2
ON T2.EMPLOYEE_NUMBER = T1.EMPLOYEE_NUMBER
AND T2.maxval > T1.maxval
WHERE (T2.maxval IS NULL)
这里Maxval是连接列。
答案 3 :(得分:0)
您指定的目标与您的结果不符,但是我会这样做(至少在DB2中如此):
select e.Employee_Number, e.Calendar, e.Performance
from Employee e
where e.Calendar in (select max(Calendar) from Employee group by Employee_Number)
答案 4 :(得分:-1)
select v1.performance,max(v1.field)
from view1 v1
group by v1.performance