Friday, March 9, 2012

How to sum Salary column with a condition

Hi,
I'd like to sum the salary for the PayDate<'5/1/1985'. Can I use one SQL
statement to get it? Please show me the SQL statement. Thank a lot.
EmployeeNo PayDate Salary
1 1/31/1985 $1,000.00
1 2/28/1985 $1,000.00
1 3/31/1985 $1,000.00
1 4/30/1985 $1,000.00
1 5/31/1985 $1,000.00
1 6/30/1985 $1,000.00"chris" <yma16@.kicon.com> wrote in message
news:eYNiCgqGFHA.3916@.TK2MSFTNGP12.phx.gbl...
> Hi,
> I'd like to sum the salary for the PayDate<'5/1/1985'. Can I use one SQL
> statement to get it? Please show me the SQL statement. Thank a lot.
> EmployeeNo PayDate Salary
> 1 1/31/1985 $1,000.00
> 1 2/28/1985 $1,000.00
> 1 3/31/1985 $1,000.00
> 1 4/30/1985 $1,000.00
> 1 5/31/1985 $1,000.00
> 1 6/30/1985 $1,000.00
>
I assume that you want to do some grouping, or else you could simply use a
WHERE clause.
select EmployeeNo,
sum(case when PayDate<'5/1/1985' then Salary else 0 end) Salary
from MyTable
group by EmployeeNo
David|||Chris,
Try....
Select Sum(Salary) as 'Total Salary', EmployeeNo
from YourTable
where PayDate < '5/1/1985'
Group By EmployeeNo
Thanks
Barry|||Thank you both for the quick help.
"chris" <yma16@.kicon.com> wrote in message
news:eYNiCgqGFHA.3916@.TK2MSFTNGP12.phx.gbl...
> Hi,
> I'd like to sum the salary for the PayDate<'5/1/1985'. Can I use one
SQL
> statement to get it? Please show me the SQL statement. Thank a lot.
> EmployeeNo PayDate Salary
> 1 1/31/1985 $1,000.00
> 1 2/28/1985 $1,000.00
> 1 3/31/1985 $1,000.00
> 1 4/30/1985 $1,000.00
> 1 5/31/1985 $1,000.00
> 1 6/30/1985 $1,000.00
>

No comments:

Post a Comment