Mysql 选择计费小时数和非计费小时数

Mysql 选择计费小时数和非计费小时数,mysql,sql,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 |

我是从数据库开始的,所以请善待我。我想写的应用程序看起来很基本,我试过了,但失败了

我的问题是: 我有这个:

表:雇员

# | 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
 ________________________________________________________

可计费小时数是指待计费行=true的小时数。非计费小时数为待计费行=false

以下是我目前正在处理的SQL查询,我使用该工具帮助我构建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, 

(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 

首先在所有表之间进行适当的联接。只是千万不要在from语句中使用逗号。然后在sum中执行条件聚合-嵌套case语句:


这假设Tobebill采用的值为0和1。将e.Id包含在group by中是为了处理两名员工姓名相同的情况。

关于如何计算计费小时数的任何线索|非计费小时数???不要混合联接样式。事实上,根本不要使用逗号连接样式。此外,考虑提供适当的DDL和/或SqLFIDLE连同期望的结果集。您如何可以加入员工与表?可计费的时间是ToBeBeldLe=真的那些。非计费小时数为ToBeBilled lines=false。您能提供一些示例数据吗?还显示了表的定义,还有ID?简直太完美了!我只需复制/粘贴您的查询,就可以了!谢谢你的帮助!仔细查看条件后=真实和=事实并非相关条件。正如@Gordon Linoff所建议的,最好使用=0和=1作为条件。@axsoverflow如果它是一个tinyint列,最好使用=0和=1,或者在插补时使用CASE。Tobebill然后。。。不插补时结束和案例。然后填写。。。你的要求完全符合我的问题。我试着把两个答案作为答案,但这是强制性的选择。抱歉。仔细查看条件后=真实和=@fthiella建议为true,它们不是相关条件。最好使用=0和=1作为条件。
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 
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;