Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 help in gettin right data

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 2009
My 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,5

if 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 be

dateadd(month,StaffAtten.MonthId-1,dateadd(year,StaffAtten.YearId-1900,0))>=dateadd(month,@FromMonthId-1,dateadd(year,@FromYearId-1900,0)) and
dateadd(month,StaffAtten.MonthId-1,dateadd(year,StaffAtten.YearId-1900,0))<dateadd(month,@ToMonthId-1,dateadd(year,@ToYearId-1900,0))+1

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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..
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-03-06 : 04:47:00
See my edited reply

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

xpandre
Posting Yak Master

212 Posts

Posted - 2009-03-06 : 05:09:33
Not very sure but..
select * from table
where
cast(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))
Go to Top of Page

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,3
UserId TotalDays Present Absent UserName
----------- ----------- ---------------------- ---------------------- --------------------------------------------------
28 853085 825115 27970 Akash
26 853085 762182.5 90902.5 Gagan
79 853085 517445 335640 RupaliS
27 853085 832107.5 20977.5 Saurabh
25 853085 769175 83910 Tahir

(5 row(s) affected)
these results can,t be true.

Go to Top of Page

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 datetime
declare @ToD datetime

set @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
Go to Top of Page

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..
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-03-06 : 05:34:15
is there an error ?
Go to Top of Page

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 Akash
26 1286620 1195717.5 90902.5 Gagan
79 1286620 895040 391580 RupaliS
27 1286620 1209702.5 76917.5 Saurabh
25 1286620 1202710 83910 Tahir

(5 row(s) affected)

Go to Top of Page

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
Go to Top of Page

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
Go to Top of Page

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 2009
28 419550 398572.5 20977.5 Akash 11 2008
28 433535 426542.5 6992.5 Akash 12 2008
26 433535 433535 0 Gagan 1 2009
26 419550 377595 41955 Gagan 11 2008
26 433535 384587.5 48947.5 Gagan 12 2008
79 433535 377595 55940 RupaliS 1 2009
79 419550 293685 125865 RupaliS 11 2008
79 433535 223760 209775 RupaliS 12 2008
27 433535 377595 55940 Saurabh 1 2009
27 419550 405565 13985 Saurabh 11 2008
27 433535 426542.5 6992.5 Saurabh 12 2008
25 433535 433535 0 Tahir 1 2009
25 419550 419550 0 Tahir 11 2008
25 433535 349625 83910 Tahir 12 2008

(15 row(s) affected)

Go to Top of Page

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 2008
28 433535 426542.5 6992.5 Akash 12 2008
26 419550 377595 41955 Gagan 11 2008
26 433535 384587.5 48947.5 Gagan 12 2008
79 419550 293685 125865 RupaliS 11 2008
79 433535 223760 209775 RupaliS 12 2008
27 419550 405565 13985 Saurabh 11 2008
27 433535 426542.5 6992.5 Saurabh 12 2008
25 419550 419550 0 Tahir 11 2008
25 433535 349625 83910 Tahir 12 2008

(10 row(s) affected)

Go to Top of Page

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 2009
28 419550 398572.5 20977.5 Akash 11 2008
28 433535 426542.5 6992.5 Akash 12 2008
26 433535 433535 0 Gagan 1 2009
26 419550 377595 41955 Gagan 11 2008
26 433535 384587.5 48947.5 Gagan 12 2008
79 433535 377595 55940 RupaliS 1 2009
79 419550 293685 125865 RupaliS 11 2008
79 433535 223760 209775 RupaliS 12 2008
27 433535 377595 55940 Saurabh 1 2009
27 419550 405565 13985 Saurabh 11 2008
27 433535 426542.5 6992.5 Saurabh 12 2008
25 433535 433535 0 Tahir 1 2009
25 419550 419550 0 Tahir 11 2008
25 433535 349625 83910 Tahir 12 2008

(15 row(s) affected)





What was your execute statement which brought back this result.
Go to Top of Page

abcd
Yak Posting Veteran

92 Posts

Posted - 2009-03-07 : 01:26:14
saket,

Select
StaffAtten.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--
Go to Top of Page
   

- Advertisement -