| Author |
Topic |
|
abcd
Yak Posting Veteran
92 Posts |
Posted - 2009-03-06 : 02:01:15
|
| Hi all...Here is a view of my problem...I have a table as Staff_attendence and many more.Basically i want data between two dates such as in nov 2008 to jan 2009My whole query is working correctly when 'fromyear' to 'toyear' is same..Problem is coming when year changes sucha as from year 2008 to year 2009 as query is not able to recognise the change in 10-2008 to 1-2009(difference from 10 t0 01)-- MY Procedure is as follows... CREATE PROCEDURE proc_Staff_BindAttendenceGroup ( @FromMonthId int, @FromYearId int, @ToMonthId int, @ToYearId int, @CompanyId int, @DeptId int ) AS BEGIN Select StaffAtten.UserId, Sum(StaffAtten.DaysInmonth) as TotalDays, sum(StaffAtten.Present) as Present, sum(StaffAtten.Absent) as Absent, ULogin.UserName ,from Staff_MonthAttendence as StaffAtten Left join User_Login as ULogin on StaffAtten.UserId=ULogin.UserId Left Join User_colink as ULink on ULogin.UserId=ULink.userId LEFT JOIN DEPT AS D ON D.DEPTID=ULink.DEPTID WHERE StaffAtten.MonthId between @FromMonthId and @ToMonthId and StaffAtten.YearId between @FromYearId and @ToYearId and ULink.CompanyId= @CompanyId and ULogin.UserLevel <>1 and ULogin.UserLevel <>2 and (@DeptId is null or ULink.DeptId=@DeptId) and ULogin.isActivated!=0 Group By StaffAtten.UserId, ULogin.UserName --Tour ORDER BY ULogin.UserName END --exec proc_Staff_BindAttendenceGroup 11, 2008,11,2008,2,5if i change my execution statemnt as 11,2008,01,2009,2,5...i am not getting any results...i know problem is coming in comparing month year difference...please throw some light on above issue... |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-03-06 : 04:03:32
|
| StaffAtten.MonthId between @FromMonthId and @ToMonthId and StaffAtten.YearId between @FromYearId and @ToYearId should bedateadd(month,StaffAtten.MonthId-1,dateadd(year,StaffAtten.YearId-1900,0))>=dateadd(month,@FromMonthId-1,dateadd(year,@FromYearId-1900,0)) anddateadd(month,StaffAtten.MonthId-1,dateadd(year,StaffAtten.YearId-1900,0))<dateadd(month,@ToMonthId-1,dateadd(year,@ToYearId-1900,0))+1 MadhivananFailing to plan is Planning to fail |
 |
|
|
abcd
Yak Posting Veteran
92 Posts |
Posted - 2009-03-06 : 04:28:12
|
| i did wht u suggested but again the problem is same..query runs for same year but when i input 11,2008,01,2009 no data is displayed...so the main problem is coming in change of year.. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-03-06 : 04:47:00
|
| See my edited replyMadhivananFailing to plan is Planning to fail |
 |
|
|
xpandre
Posting Yak Master
212 Posts |
Posted - 2009-03-06 : 05:09:33
|
Not very sure but..select * from tablewherecast(cast(StaffAtten.MonthId as varchar)+'/01/'+cast(StaffAtten.YearId as varchar) as datetime) between cast(cast(@frommonthid as varchar)+'/01/'+cast(@fromyearid as varchar) as datetime) and dateadd(month,1,cast(cast(@tomonthid as varchar)+'/01/'+cast(@toyearid as varchar) as datetime)) |
 |
|
|
abcd
Yak Posting Veteran
92 Posts |
Posted - 2009-03-06 : 05:10:21
|
| sir i did your suggested enhancements....but the results i am getting are not correct...i am getting these results for executing the procedure proc_Staff_BindAttendenceGroup.--exec proc_Staff_BindAttendenceGroup 11, 2008,12,2008,1,3UserId TotalDays Present Absent UserName----------- ----------- ---------------------- ---------------------- --------------------------------------------------28 853085 825115 27970 Akash26 853085 762182.5 90902.5 Gagan79 853085 517445 335640 RupaliS27 853085 832107.5 20977.5 Saurabh25 853085 769175 83910 Tahir(5 row(s) affected)these results can,t be true. |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-06 : 05:19:56
|
try this,CREATE PROCEDURE proc_Staff_BindAttendenceGroup ( @FromMonthId int, @FromYearId int, @ToMonthId int, @ToYearId int, @CompanyId int, @DeptId int ) AS BEGIN declare @FromD datetimedeclare @ToD datetimeset @FromD=convert(datetime,convert(varchar,@FromYearId)+convert(varchar,@FromMonthId)+'01')set @ToD=convert(datetime,convert(varchar,@ToYearId)+convert(varchar,@ToMonthId)+'01')Select StaffAtten.UserId, Sum(StaffAtten.DaysInmonth) as TotalDays, sum(StaffAtten.Present) as Present, sum(StaffAtten.Absent) as Absent, ULogin.UserName from Staff_MonthAttendence as StaffAtten Left join User_Login as ULogin on StaffAtten.UserId=ULogin.UserId Left Join User_colink as ULink on ULogin.UserId=ULink.userId LEFT JOIN DEPT AS D ON D.DEPTID=ULink.DEPTID WHERE convert(Datetime,convert(varchar,StaffAtten.YearId)+convert(varchar,StaffAtten.MonthId+'01')) between @FromD and @ToD and ULink.CompanyId= @CompanyId and ULogin.UserLevel <>1 and ULogin.UserLevel <>2 and (@DeptId is null or ULink.DeptId=@DeptId) and ULogin.isActivated!=0 Group By StaffAtten.UserId, ULogin.UserName --Tour ORDER BY ULogin.UserName END --exec proc_Staff_BindAttendenceGroup 11, 2008,11,2008,2,5 |
 |
|
|
abcd
Yak Posting Veteran
92 Posts |
Posted - 2009-03-06 : 05:30:30
|
| i am sorry sir i didnt got my problem solved by your suggestion....madhivanan sir's code can work for me with some changes...any further help is really appreciated.. |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-06 : 05:34:15
|
| is there an error ? |
 |
|
|
abcd
Yak Posting Veteran
92 Posts |
Posted - 2009-03-06 : 05:41:52
|
| these are the results saket i am getting after executing your changes...UserId TotalDays Present Absent UserName----------- ----------- ---------------------- ---------------------- --------------------------------------------------28 1286620 1251657.5 34962.5 Akash26 1286620 1195717.5 90902.5 Gagan79 1286620 895040 391580 RupaliS27 1286620 1209702.5 76917.5 Saurabh25 1286620 1202710 83910 Tahir(5 row(s) affected) |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-06 : 05:47:08
|
| Can you print these 2 columns as well ??StaffAtten.YearId,convertStaffAtten.MonthId |
 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2009-03-06 : 06:22:45
|
You can avoid casting by using CASE.Try replacing your original WHERE clause with something like:WHERE StaffAtten.YearId BETWEEN @FromYearId AND @ToYearId AND StaffAtten.MonthId BETWEEN CASE WHEN StaffAtten.YearId = @FromYearId THEN @FromMonthId ELSE 1 END AND CASE WHEN StaffAtten.YearId = @ToYearId THEN @ToMonthId ELSE 12 END AND ULink.CompanyId = @CompanyId AND ULogin.UserLevel <> 1 AND ULogin.UserLevel <> 2 AND (@DeptId IS NULL OR ULink.DeptId = @DeptId) AND ULogin.isActivated <> 0 |
 |
|
|
abcd
Yak Posting Veteran
92 Posts |
Posted - 2009-03-06 : 06:50:35
|
| Sir these are the results after i brought stffatten.monthid and staffatten.yearid...UserId TotalDays Present Absent UserName monthid yearid----------- ----------- ---------------------- ---------------------- -------------------------------------------------- ----------- -----------28 433535 426542.5 6992.5 Akash 1 200928 419550 398572.5 20977.5 Akash 11 200828 433535 426542.5 6992.5 Akash 12 200826 433535 433535 0 Gagan 1 200926 419550 377595 41955 Gagan 11 200826 433535 384587.5 48947.5 Gagan 12 200879 433535 377595 55940 RupaliS 1 200979 419550 293685 125865 RupaliS 11 200879 433535 223760 209775 RupaliS 12 200827 433535 377595 55940 Saurabh 1 200927 419550 405565 13985 Saurabh 11 200827 433535 426542.5 6992.5 Saurabh 12 200825 433535 433535 0 Tahir 1 200925 419550 419550 0 Tahir 11 200825 433535 349625 83910 Tahir 12 2008(15 row(s) affected) |
 |
|
|
abcd
Yak Posting Veteran
92 Posts |
Posted - 2009-03-06 : 06:55:27
|
| ifor sir i did changed my query according to your suggestion .These are the results after executing your query...UserId TotalDays Present Absent UserName monthid yearid----------- ----------- ---------------------- ---------------------- -------------------------------------------------- ----------- -----------28 419550 398572.5 20977.5 Akash 11 200828 433535 426542.5 6992.5 Akash 12 200826 419550 377595 41955 Gagan 11 200826 433535 384587.5 48947.5 Gagan 12 200879 419550 293685 125865 RupaliS 11 200879 433535 223760 209775 RupaliS 12 200827 419550 405565 13985 Saurabh 11 200827 433535 426542.5 6992.5 Saurabh 12 200825 419550 419550 0 Tahir 11 200825 433535 349625 83910 Tahir 12 2008(10 row(s) affected) |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-06 : 06:57:45
|
quote: Originally posted by abcd Sir these are the results after i brought stffatten.monthid and staffatten.yearid...UserId TotalDays Present Absent UserName monthid yearid----------- ----------- ---------------------- ---------------------- -------------------------------------------------- ----------- -----------28 433535 426542.5 6992.5 Akash 1 200928 419550 398572.5 20977.5 Akash 11 200828 433535 426542.5 6992.5 Akash 12 200826 433535 433535 0 Gagan 1 200926 419550 377595 41955 Gagan 11 200826 433535 384587.5 48947.5 Gagan 12 200879 433535 377595 55940 RupaliS 1 200979 419550 293685 125865 RupaliS 11 200879 433535 223760 209775 RupaliS 12 200827 433535 377595 55940 Saurabh 1 200927 419550 405565 13985 Saurabh 11 200827 433535 426542.5 6992.5 Saurabh 12 200825 433535 433535 0 Tahir 1 200925 419550 419550 0 Tahir 11 200825 433535 349625 83910 Tahir 12 2008(15 row(s) affected)
What was your execute statement which brought back this result. |
 |
|
|
abcd
Yak Posting Veteran
92 Posts |
Posted - 2009-03-07 : 01:26:14
|
| saket,SelectStaffAtten.UserId,Sum(StaffAtten.DaysInmonth) as TotalDays,sum(StaffAtten.Present) as Present,sum(StaffAtten.Absent) as Absent,ULogin.UserName ,Staffatten.monthId--i added these two things in select,staffatten.yearid-- |
 |
|
|
|