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 2000 Forums
 Transact-SQL (2000)
 DateAdd Function

Author  Topic 

stormcandi
Starting Member

46 Posts

Posted - 2007-05-10 : 18:07:49
Hello all, I have this Stored Procedure:
	SELECT 
logs.UserName,
logs.CID,
logs.logon,
logs.logoff,
logs.tot_min as day_min,
sch.begweek,
sch.RecID as wkRecID,
logs.offline
FROM
stu_logs logs INNER JOIN
[Term 5 Courses] course ON course.RecID = logs.CID LEFT JOIN
schoolweeks sch ON Cast(logs.logon as datetime) BETWEEN sch.begweek AND DATEADD(dd, 7, sch.begweek)
Where
logs.UserName = 'jaban6988'
and logs.CID = '19'
and logs.[yr/Term] = ('2006' + '5')
and Current_Timestamp not between sch.begweek AND DATEADD(dd, 7, sch.begweek)


That returns this result set:
UserName,CID,Login,Logoff,min,begweek
tester,19,2007-04-30 00:00:00.000,2007-04-30 03:00:00.000,180,2007-04-23 00:00:00.000
tester,19,2007-04-30 00:00:00.000,2007-04-30 03:00:00.000,180,2007-04-30 00:00:00.000

I am having a hard time trying to figure out how 4/30 can exist in the week beginning 4/23 AND in the week beginning 4/30.

Can anyone help?

I hope. I appreciate it!

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-05-10 : 18:30:25
You have a logical error in your left join.

Try recoding it like this:

LEFT JOIN
schoolweeks sch
ON Cast(logs.logon as datetime) >= sch.begweek AND
Cast(logs.logon as datetime) < DATEADD(dd, 7, sch.begweek)


CODO ERGO SUM
Go to Top of Page

stormcandi
Starting Member

46 Posts

Posted - 2007-05-10 : 18:40:29
quote:
Originally posted by Michael Valentine Jones

You have a logical error in your left join.

Try recoding it like this:

LEFT JOIN
schoolweeks sch
ON Cast(logs.logon as datetime) >= sch.begweek AND
Cast(logs.logon as datetime) < DATEADD(dd, 7, sch.begweek)


CODO ERGO SUM



I was working on it while waiting for an answer, and have fixed it using:

LEFT JOIN
schoolweeks sch
ON dbo.F_START_OF_WEEK(CAST(logs.logon as datetime),2) = sch.begweek


Now the other problem I have is in calculating the time for the week as a whole. This is the query:

SELECT
sch.begweek,
logs.CID,
logs.UserName,
SUM(logs.tot_min) AS wk_min
FROM
stu_logs logs INNER JOIN
[Term 5 Courses] course ON course.RecID = logs.CID LEFT JOIN
schoolweeks sch ON Cast(logs.logon as datetime) BETWEEN sch.begweek AND DATEADD(dd, 7, sch.begweek) LEFT JOIN
VerTime ver ON sch.recID = ver.week and logs.UserName = ver.UserName and logs.CID = ver.CID Left Join
PrblmTime prblm ON sch.recID = prblm.week and logs.UserName = prblm.UserName and logs.CID = prblm.CID
WHERE
logs.UserName = 'tester'
and logs.CID = '19'
and logs.[yr/Term] = ('2006' + '5')
and Current_Timestamp not between sch.begweek AND DATEADD(dd, 7, sch.begweek)
GROUP BY
ver.RecID,
prblm.RecID,
prblm.Active,
logs.UserName,
sch.begweek,
sch.RecID,
logs.CID
Order By sch.RecID


This is the results of that query:
week_start,CID,UserName,Tot_Min
2007-04-23 00:00:00.000,19,tester,300
2007-04-30 00:00:00.000,19,tester,360

The total minutes for both weeks includes the 180 minutes from 4/30. How do I make sure the correct week is getting the correct total for its days and not including any weeks outside of it.
Go to Top of Page

stormcandi
Starting Member

46 Posts

Posted - 2007-05-10 : 18:53:35
Ok, I figured it out. I just replaced the part:

schoolweeks sch ON Cast(logs.logon as datetime) BETWEEN sch.begweek AND DATEADD(dd, 7, sch.begweek)

with:

schoolweeks sch ON dbo.F_START_OF_WEEK(CAST(logs.logon as datetime),2) = sch.begweek

It is now calculating the time correctly for each week. Thanks a lot for your help!

Go to Top of Page
   

- Advertisement -