Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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);
Posted
Updated 15-Oct-14 23:51pm
v2
Comments
KaushalJB 16-Oct-14 6:02am    
You mean to say that you want SqlCommand commandFinal = command1 + command2 + command3 ? Is this you need ?
firaso2014 16-Oct-14 6:43am    
no
I want to use alias in case1 with alias in case2 and sum them without repeat whole case
must I use subquery?
how to use it?

You can achieve that by using subquery:

SQL
SELECT Sum1, Sum2, Sum1+Sum2 AS Sum3
FROM (
    SELECT SUM(Field1) AS Sum1, SUM(Field2) AS Sum2
    FROM TableName
    GROUP BY ...
) AS T
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900