firas shaar

firas shaar

  • NA
  • 15
  • 17.7k

How to use alias and sum them together in select query

Oct 16 2014 5:46 AM
I  have the following queries below. I'd like to use the aliases and add them together but SQL does not allow that. Any suggestions on how I can go about this without repeating the queries again? Using Microsoft SQL Server 2008

   SqlCommand command = new SqlCommand(@"SELECT Workers.Full_Name,Workers.Salary,
 CASE WHEN (SUM(JobHours.Additional_Time)) > 0 THEN (SUM(JobHours.Additional_Time)) ELSE (0) END AS Plusovertime,
            CASE WHEN (SUM(JobHours.Additional_Time)) > 0 THEN ROUND(((Workers.Salary/300)*1.5),0) ELSE (0) END AS PlusRental,            
(Plusovertime+PlusRental) AS PULSNET

 FROM Workers INNER JOIN JobHours ON Workers.Worker_ID = JobHours.Worker_ID

 WHERE  Jobhours.Start_Work BETWEEN @startdatetime AND @enddatetime
            GROUP BY Workers.Worker_ID, Workers.Full_Name,Workers.Salary
            ORDER BY Workers.Worker_ID;", conn);
          

Answers (1)