title: Select the amount of billable hours and non-billable hours
body: <p>I started in the database, so be kind to me please. The application that I want to write seems basic, I tried, but I failed.</p>
  
  <p>Here is my problem:
  I have this:</p>
  
  <p>Table: Employee</p>
  
  <pre><code># | 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 | 
  </code></pre>
  
  <p>Table : Imputation</p>
  
  <pre><code># | 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 | 
  </code></pre>
  
  <p>Table : TimeSheet</p>
  
  <pre><code># | 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 | 
  </code></pre>
  
  <p>And I want to achieve the following result :</p>
  
  <pre><code>________________________________________________________
  Name     | Billable hours | Non-billable hours | Total hours
  ________________________________________________________
  John Doe | 872            | 142                | 1014
   ________________________________________________________
  </code></pre>
  
  <p>Billable hours are those with ToBeBilled lines = true. Non-billable hours are ToBeBilled lines = false.</p>
  
  <p>Here is my SQL query that I'm currently working on (I use <a href="http://www.activedbsoft.com/overview-querytool.html" rel="nofollow">FlySpeed ​​SQL Query</a> tool to help me build my SQL queries) :</p>
  
  <pre><code>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
  </code></pre>
  
  <hr>
  
  <p>After help, here is the final query :</p>
  
  <pre><code>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
  </code></pre>