获取最大工作时间的日期
Get date of max hours worked
Tables:
员工(ID,姓名)
EmployeeHours ( EmployeeID ,Date, Hours )
这是我的要求:
Write a sql for following results for a year EmployeeID ,
TotalHoursWorked , Date and Hours ( Date and Hours when Employee worked
most )
我写了以下查询:
select e.id, e.name, sum(eh.hours) as totalHoursWorked, max(hours) hour
from shoplist.emp e, shoplist.empHours eh where e.id = eh.empId
group by eh.empId;
我可以使用 maxHour 获取 ID、名称、totalHoursWorked,但我对如何获取最长工作时间的日期感到有点困惑。有人可以帮我解决这个问题吗?
示例数据:
Emp Table
id, name
'1','akhil'
'2','preethi'
'3','gopi'
工作时间Table
id, empId, hours, date
'1','1','3','2022-05-12'
'2','1','5','2022-05-11'
'3','1','4','2022-05-10'
'4','2','2','2022-05-12'
'5','2','4','2022-05-10'
'6','3','3','2022-05-09'
'7','3','5','2022-05-08'
预计
id, name, totalHoursWorked, maxHours, maxHourDate
'1','akhil','12','5','2022-05-12'
'2','preethi','6','4','2022-05-12'
'3','gopi','8','5','2022-05-09'
您可以使用您的查询结果与原始查询结果合并 table
select distinct eh1.empId, eh1.date, t.hours, t.name, t.totalHoursWorked
from shoplist.empHours eh1 inner join (
select e.id, e.name, sum(eh.hours) as totalHoursWorked, max(eh.hours) hours
from shoplist.emp e
inner join shoplist.empHours eh on e.id = eh.empId group by eh.empId
) t on t.id = eh1.empId AND t.hours = eh1.hours
对于具有相同小时数的多个日期的相同用途,您可以通过这种方式选择最大值或最小值
select eh1.empId, max(eh1.date), t.hours, t.name, t.totalHoursWorked
from shoplist.empHours eh1 inner join (
select e.id, e.name, sum(eh.hours) as totalHoursWorked, max(eh.hours) hours
from shoplist.emp e
inner join shoplist.empHours eh on e.id = eh.empId group by eh.empId
) t on t.id = eh1.empId AND t.hours = eh1.hours
group by eh1.empId
Tables: 员工(ID,姓名) EmployeeHours ( EmployeeID ,Date, Hours )
这是我的要求:
Write a sql for following results for a year EmployeeID , TotalHoursWorked , Date and Hours ( Date and Hours when Employee worked most )
我写了以下查询:
select e.id, e.name, sum(eh.hours) as totalHoursWorked, max(hours) hour
from shoplist.emp e, shoplist.empHours eh where e.id = eh.empId
group by eh.empId;
我可以使用 maxHour 获取 ID、名称、totalHoursWorked,但我对如何获取最长工作时间的日期感到有点困惑。有人可以帮我解决这个问题吗?
示例数据:
Emp Table
id, name
'1','akhil'
'2','preethi'
'3','gopi'
工作时间Table
id, empId, hours, date
'1','1','3','2022-05-12'
'2','1','5','2022-05-11'
'3','1','4','2022-05-10'
'4','2','2','2022-05-12'
'5','2','4','2022-05-10'
'6','3','3','2022-05-09'
'7','3','5','2022-05-08'
预计
id, name, totalHoursWorked, maxHours, maxHourDate
'1','akhil','12','5','2022-05-12'
'2','preethi','6','4','2022-05-12'
'3','gopi','8','5','2022-05-09'
您可以使用您的查询结果与原始查询结果合并 table
select distinct eh1.empId, eh1.date, t.hours, t.name, t.totalHoursWorked
from shoplist.empHours eh1 inner join (
select e.id, e.name, sum(eh.hours) as totalHoursWorked, max(eh.hours) hours
from shoplist.emp e
inner join shoplist.empHours eh on e.id = eh.empId group by eh.empId
) t on t.id = eh1.empId AND t.hours = eh1.hours
对于具有相同小时数的多个日期的相同用途,您可以通过这种方式选择最大值或最小值
select eh1.empId, max(eh1.date), t.hours, t.name, t.totalHoursWorked
from shoplist.empHours eh1 inner join (
select e.id, e.name, sum(eh.hours) as totalHoursWorked, max(eh.hours) hours
from shoplist.emp e
inner join shoplist.empHours eh on e.id = eh.empId group by eh.empId
) t on t.id = eh1.empId AND t.hours = eh1.hours
group by eh1.empId