选择可计费小时数和不可计费小时数

时间:2014-03-24 10:23:57

标签: mysql sql

我从数据库开始,请对我好心。我想写的应用程序似乎是基本的,我试过,但我失败了。

这是我的问题: 我有这个:

表:员工

# | 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 lines = true的时间。不可计费小时数是ToBeBilled lines = 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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

试试这个,

Select Employee.Name, 

(Select sum(Imputation.Hours) from TimeSheet where (TimeSheet.Employee_Id =  Employee.Id) AND (Imputation.ToBeBilled = 'true')) as BillableHours,

 (Select sum(Imputation.Hours) from TimeSheet where (TimeSheet.Employee_Id =  Employee.Id) AND (Imputation.ToBeBilled = 'false')) as NonBillableHours,

 (Select sum(Imputation.Hours) from TimeSheet where (TimeSheet.Employee_Id =  Employee.Id) ) as TotalHours

FROM Employee 

答案 2 :(得分:0)

首先在所有表之间进行正确的连接。只需永远from语句中使用逗号。然后进行条件聚合 - 在case中嵌套sum()语句:

Select e.Name, Sum( i.Hours) as TotalHours,
       sum(case when i.ToBeBilled = 1 then i.Hours else 0 end) as Billable,
       sum(case when i.ToBeBilled = 0 then i.Hours else 0 end) as NonBillable
From Employee e Inner Join
     TimeSheet ts
     On ts.Employee_Id =  e.Id Inner Join
     Imputation i
     on i.TimeSheetID = ts.TimeSheetId
Group By e.Name, e.id
Order By Employee.Name;

这假定ToBeBilled所采用的值为01。在e.Id中包含group by是为了处理两名员工具有相同名称的情况。