我希望每个员工部门获得最多的星级,所以从表csereduxresponses中添加每个部门的星星,然后使用employee.cse_dept(每个部门的员工#of)对其进行划分。 employedept = dept_id,employee = emp_id。 我制作了一个模拟表来简化。
create table csereduxresponds (employeedept int, employee int, stars int);
insert into csereduxresponds values(1,1,1);
insert into csereduxresponds values(1,1,0);
insert into csereduxresponds values(1,1,1);
insert into csereduxresponds values(1,2,1);
insert into csereduxresponds values(1,2,0);
insert into csereduxresponds values(3,3,1);
insert into csereduxresponds values(3,3,1);
insert into csereduxresponds values(4,3,1);
insert into csereduxresponds values(4,3,0);
insert into csereduxresponds values(4,3,1);
insert into csereduxresponds values(4,3,1);
insert into csereduxresponds values(5,4,0);
insert into csereduxresponds values(5,4,1);
insert into csereduxresponds values(5,4,1);
create table employee ( dept_id int,emp_id int,cse_dept int);
insert into employee values (1,1,14);
insert into employee values (1,2,14);
insert into employee values (3,3,8);
insert into employee values (5,4,9);
insert into employee values (6,5,10);
create table csereduxdepts(csedept_id int, dept_name varchar(25));
insert into csereduxdepts(1,'dapartmen one');
insert into csereduxdepts(2,'dapartmen two');
insert into csereduxdepts(3,'dapartmen 3');
insert into csereduxdepts(4,'dapartmen 4');
insert into csereduxdepts(5,'dapartmen 5');
insert into csereduxdepts(6,'dapartmen 6');
我有一个类似的查询,它获取每个员工的total_stars,但不是部门:
select a.employee, a.execoffice_status,a.employeedept, b.csedept_id as department_id , b.csedept_name as department_name,
SUM(execoffice_status) as total_stars,RANK() OVER(ORDER BY SUM(execoffice_status)desc )as rnk
from intranet.dbo.CSEReduxResponses a join intranet.dbo.CSEReduxDepts b
on b.csedept_id = a.employeedept
group by employee,execoffice_status,employeedept,csedept_id,csedept_name
order by rnk
我放弃添加另一个具有部门名称的表。 在这里,我想输出部门名称和stars / cse_dept
答案 0 :(得分:0)
根据您提供的内容,这里是每个部门每位员工的星标汇总除以员工每个部门的静态值(cse_dept
)。 PerCSE显示为百分比。
SELECT employeedept
, employee
, SUM(stars) NumStars
, MAX(cse_dept) EmployeeDeptCount
, SUM(stars) * 100.00 / MAX(cse_dept) AS PerCSE
FROM employee e
JOIN csereduxresponds c
ON employeedept = dept_id
AND employee = emp_id
GROUP BY employeedept
, employee
这是根据提供的条件进行的连接。
SELECT *
FROM employee e
JOIN csereduxresponds c
ON employeedept = dept_id
AND employee = emp_id
答案 1 :(得分:0)
;WITH CTE
AS
(
select employeedept
, employe From employee e
)
Select c.employeedept
, c.employe
, SUM(cc.stars) NumStars
, MAX(cc.cse_dept) EmployeeDeptCount
, SUM(cc.stars) * 100.00 / MAX(cc.cse_dept) AS PerCSE
From CTE C
INNER JOIN csereduxresponds cc
ON C.employeedept = cc.dept_id
AND C.employee = CC.emp_id