我是从数据库开始的,所以请善待我。我想写的应用程序看起来很简单,我尝试过,但失败了。
这是我的问题: 我有这个:
表:员工
# | Colonne | Type | Interclassement | Attributs | Null | Défaut | Extra | Action
1 | Id | int(11) | no | Aucune | AUTO_INCREMENT
2 | Name | varchar(50) | latin1_swedish_ci | yes | NULL
3 | Firstname | varchar(50) | latin1_swedish_ci | yes | NULL |
4 | IdNumber | int(11) | yes | NULL
5 | Mail | varchar(50) | latin1_swedish_ci | no | Aucune |
6 | PassWord | varchar(50) | latin1_swedish_ci | no | Aucune |
7 | Site | varchar(50) | latin1_swedish_ci | yes | NULL |
8 | Entrance | date | yes | NULL |
9 | Departure | date | yes | NULL |
10 | Car_Id | int(11) | yes | NULL |
11 | Profil_Id | int(11) | yes | NULL |
表:插补
# | Colonne | Type | Interclassement | Attributs | Null | Défaut | Extra | Action
1 | Id | int(11) | | | no | Aucune | AUTO_INCREMENT
2 | Hours | int(11) | | | yes | NULL |
3 | Description | varchar(256) | latin1_swedish_ci | | yes | NULL |
4 | ToBeBilled | tinyint(1) | | | yes | 1 |
5 | BillNumber | int(11) | | | yes | NULL |
6 | Day | date | | | yes | NULL |
7 | TimeSheet_Id | int(11) | | | no | Aucune |
8 | Project_Id | int(11) | | | no | Aucune |
9 | automatic | tinyint(1) | | | no | 0 |
表:时间表
# | Colonne | Type | Interclassement | Attributs | Null | Défaut | Extra | Action
1 | Id | int(11) | | | no | Aucune | AUTO_INCREMENT
2 | Month | int(2) | | | yes | NULL |
3 | Year | int(4) | | | yes | NULL |
4 | Filled | tinyint(1) | | | yes | 0 |
5 | Closed | tinyint(1) | | | yes | 0
6 | Employee_Id | int(11) | | | no | Aucune |
我想达到以下结果:
________________________________________________________
Name | Billable hours | Non-billable hours | Total hours
________________________________________________________
John Doe | 872 | 142 | 1014
________________________________________________________
计费时间是指 ToBeBilled 行 = true 的时间。不可计费时间为 ToBeBilled 行 = false。
这是我当前正在处理的 SQL 查询(我使用 FlySpeed SQL Query 工具来帮助我构建 SQL 查询):
Select
Employee.Name,
Sum( Imputation.Hours),
Imputation.ToBeBilled
From
Employee Inner Join
TimeSheet On TimeSheet.Employee_Id = Employee.Id,
Imputation
Where
Imputation.ToBeBilled = 'true'
Group By
Employee.Name, Imputation.ToBeBilled
Order By
Employee.Name
<小时/>
经过帮助,这是最终的查询:
Select
Employee.Name As Name,
Sum(Case When Imputation.ToBeBilled = '1' Then Imputation.Hours End) As `Billable`,
Sum(Case When Imputation.ToBeBilled = '0' Then Imputation.Hours End) As `NonBillable`,
Sum(Imputation.Hours) As `Total`
From
Employee Inner Join
TimeSheet On TimeSheet.Employee_Id = Employee.Id Inner Join
Imputation On Imputation.TimeSheet_Id = TimeSheet.Id
Group By
Employee.Name, Employee.Id
Order By
Name
最佳答案
SELECT
Employee.Name,
Sum(CASE WHEN Imputation.ToBeBilled = 'true' THEN Imputation.Hours END) As Billable_hours,
Sum(CASE WHEN Imputation.ToBeBilled != 'true' THEN Imputation.Hours END) As NonBillable_hours,
SUM(Imputation.Hours) As total_hours
FROM
Employee INNER JOIN TimeSheet
On TimeSheet.Employee_Id = Employee.Id
INNER JOIN Imputation
ON Imputation.TimeSheet_Id=TimeSheet.id
GROUP BY
Employee.Id, Employee.Name
ORDER BY
Employee.Name
关于mysql - 选择计费小时数和非计费小时数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22606628/