Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello sir, i want to show sum of total earning, deduction between financial year and i wrote below query but it show wrong output so please suggest me

Parameters:
frmYr =2013
toYr =2014
frmMonth =3
toMonth =4


Query:

SQL
select
s.compCode,
s.compName, 
s.Location,
s.empId,
s.empName,
sum(s.BasicSalary) as BasicSalary,
sum(totalEarning) as totalEarning,
sum(totalDeduction) as totalDeduction,
sum(totalGrossSalary) as totalGrossSalary
from PAY_Process_Salary s  where s.compCode=@compCode  and s.TranMonth between @frmMonth and @toMonth and s.TranYear between @frmYr and @toYr GROUP BY s.compCode,s.compName,s.empId,s.empName,s.location order by s.empId
Posted
Updated 20-May-14 21:29pm
v2
Comments
King Fisher 21-May-14 3:33am    
what is this ?
s.compCode=@compCode

1 solution

The results are incorrect because you are comparing the month independently from the year so only month 3 and 4 will be included in your results for either year. i.e. your totals will reflect 2013/03, 2013/04, 2014/03 & 2014/04
What you need to do is create a date range from your parameters and then create a date from your data and then check to see if it falls within the desired range.

So your where clause should look something like:

SQL
where convert(datetime, s.tranyear + s.tranmonth + s.trandate) between 
          convert(datetime, '20130401') and convert(datetime, '20140331')


Be aware that different date cultures need different parameters to the convert routine so you may need to do a bit more digging.

Good luck
 
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