title: Calculate salary by year
body: <p>I want to calculate the salary of employees for each year. Let's say if an employee is in company for 1.5 years since July 2016 than I want to show the records in 2 rows like salary of 2016 and 2017 separately. If monthly salary is <code>20000</code> so for <code>18 months</code> this is how result should be displayed.</p>
  
  <pre><code>EmpID  Year   MonthlySalary  YearlySalary  TotalSalary
  1      2016   20000          120000        120000 (because was hired in jul)
  1      2017   20000          240000        360000
  2      2017   18000          216000        216000
  </code></pre>
  
  <p>I have a query that calculates the salary per month, years and total salary as well. I am calculating the total salary using months.</p>
  
  <pre><code>CREATE TABLE EmployeeInfo (
  EmpID Int,
  FirstName Varchar(25),
  LastName Varchar(25),
  MonthlySalary Int,
  DOJ Date);
  
  
  INSERT INTO EmployeeInfo VALUES (1, 'Ahmad', 'Usman', '20000', '2016-06-01');
  INSERT INTO EmployeeInfo VALUES (2, 'Erick', 'Ortiz', '18000', '2017-01-01');
  
  
  SELECT [EmployeeName] = [EI].[FirstName] + ' ' + [EI].[LastName],
         [EI].[DOJ],
         [MonthlySalary] = [EI].MonthlySalary,
         [EXPERIENCE] = ( CONVERT(VARCHAR(2), Datediff(YEAR, [DOJ], Getdate())) ),
         [TotalSalary] = ( ( CONVERT(VARCHAR(2), Datediff(MONTH, [DOJ], Getdate())) ) * ( Isnull(NULL, [EI].MonthlySalary) ) )
  FROM   [EmployeeInfo] [EI] 
  </code></pre>