考虑下表
Designation(empPosMapId, empName, designation, department, job description, dateassigned)
指定可以是" JUNIOR"," SENIOR"和" DIRECTOR"部门可以是众多部门之一。 如果未提及部门,则表示该人员持有所有部门的指定名称
每个部门可以有多个人担任相同的角色。此外,一个人可以在每个部门担任多个角色。
样本数据
empPosId| empName | designation | department | desc | date
--------|---------|-------------|------------|-------|-------------
1 | per1 | DIRECTOR | dept1 | blah1 | 2015-12-23
2 | per1 | SENIOR | (NULL) | blah1 | 2015-12-23
3 | per1 | JUNIOR | dept2 | blah1 | 2015-12-23
4 | per2 | SENIOR | dept1 | blah1 | 2015-12-23
5 | per2 | JUNIOR | (NULL) | blah1 | 2015-12-23
6 | per3 | DIRECTOR | (NULL) | blah1 | 2015-12-23
7 | per4 | JUNIOR | dept2 | blah1 | 2015-12-23
名称排名为JUNIOR< SENIOR<导向器 此外,特定于部门的角色会覆盖整体角色。 (即,per1具有SENIOR的整体角色,但是dept1是DIRECTOR因此,dert1 per1将是DIRECTOR
我需要一个查询来获取每个人在某个部门持有的最高指定的详细信息。
即,对于dept1
empPosId| empName | designation | department | desc | date
--------|---------|-------------|------------|-------|-------------
1 | per1 | DIRECTOR | dept1 | blah1 | 2015-12-23
4 | per2 | SENIOR | dept1 | blah1 | 2015-12-23
6 | per3 | DIRECTOR | (NULL) | blah1 | 2015-12-23
同样适用于dept2,
empPosId| empName | designation | department | desc | date
--------|---------|-------------|------------|-------|-----------
2 | per1 | SENIOR | (NULL) | blah1 | 2015-12-23
5 | per2 | JUNIOR | (NULL) | blah1 | 2015-12-23
6 | per3 | DIRECTOR | (NULL) | blah1 | 2015-12-23
7 | per4 | JUNIOR | dept2 | blah1 | 2015-12-23
我有一个工作查询,它使用case语句通过分配数字并选择编号最大的行来确定最高指定。 问题是它太大了,我需要简化它并在hibernate中使用它:|
(假设deptInput是用户感兴趣的部门)
select * from Designation where empPosId in (
select a.empPosId from (
select *, case
WHEN department=<deptInput> THEN 4
WHEN department is NULL AND DESIGNATION = "DIRECTOR" THEN 3
WHEN department is NULL AND DESIGNATION = "SENIOR" THEN 2
WHEN department is NULL AND DESIGNATION = "JUNIOR" THEN 1
ELSE 0 END AS "priority"
FROM Designation d where department is NULL or department=<deptInput>
)a where priority = (
select max( case
WHEN department=<deptInput> THEN 4
WHEN department is NULL AND DESIGNATION = "DIRECTOR" THEN 3
WHEN department is NULL AND DESIGNATION = "SENIOR" THEN 2
WHEN department is NULL AND DESIGNATION = "JUNIOR" THEN 1
ELSE 0 END
)
FROM Designation d where empName=a.empName GROUP BY empName
)
)
无法修改表格结构。
任何人都可以帮助简化查询,因为我需要使用它来从Java查询。或者有更简单的方法在休眠中做同样的事情?