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.
Author |
Topic |
Sleepless
Starting Member
9 Posts |
Posted - 2008-03-24 : 14:05:46
|
Hi there. New to the forums, been lurking for a bit though. Hope to get some syntax help. I have a table created that assigns a sequence number for each business day. In this table are absences, I compare the absence date with the assigned sequence number for that day and insert it for that record. What I am trying to find is anyone with 10 consecutive days absent by checking the sequence number. I was able to do this in Excel by creating a formula that looks at the previous line, checks to see if it is the same person, and that the current rows sequence is 1+ the previous row and adds 1 to the count...otherwise the count starts over again.This is proving to be difficult (for me anyway) to reproduce in SQL. I need it in SQL so I can create a SQL custom report in Reporting Services. I have read that you can use variables to store previous row data but I know I am missing something. This is uncharted waters for me. I'm assuming that I am missing some kind of loop statement to get the counter to add to itself. Right now the statment correctly assigns a 1 if there is a consecutive day, it just doesn't add 1 more to the previous row if it is consecutive...just another 1.Any help would be appreciated!Thanks,BobDECLARE @count intSET @count = 0select a.schtype,a.schname,a.stugr,a.stuln,a.stufn,a.stumn,a.stunum,a.abstype,a.absdate,a.dayseq,a.consday,CASE WHEN (select count(t.dayseq) from TenDayConsecutive t where t.stunum=a.stunum and t.dayseq=(a.dayseq-1))=1 THEN @count+1 ELSE 0 END as counterfrom TenDayConsecutive a |
|
ee_rlee
Starting Member
1 Post |
Posted - 2008-03-25 : 04:19:56
|
hi, try thisSELECT *FROM Employees EWHERE EXISTS(SELECT 1FROM Absents AWHERE A.EmpID=E.EmpID AND (SELECT COUNT(*) FROM Absents WHERE EmpID=A.EmpID AND SeqID Between A.SeqID AND A.SeqID+9)>=10) |
 |
|
Sleepless
Starting Member
9 Posts |
Posted - 2008-03-25 : 10:30:59
|
OMFG!!! You ROCK!! It worked!! I've been struggling with for a couple of days racking my brain. Thank you so much for your help, I really appreciate it!!Bob |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-25 : 11:32:20
|
[code]-- Prepare sample dataDECLARE @Sample TABLE (EmpID INT, dt DATETIME)INSERT @SampleSELECT 1, '20080325' UNION ALLSELECT 1, '20080326' UNION ALLSELECT 1, '20080327' UNION ALLSELECT 2, '20080301' UNION ALLSELECT 2, '20080309' UNION ALLSELECT 2, '20080312' UNION ALLSELECT 3, '20080303' UNION ALLSELECT 3, '20080304' UNION ALLSELECT 3, '20080315'SELECT q.EmpID, q.dtFROM ( SELECT EmpID, dt, dt + 1 AS dtFrom, dt + 10 AS dtTo FROM @Sample ) AS qLEFT JOIN @Sample AS s ON s.EmpID = q.EmpID AND s.dt BETWEEN q.dtFrom AND q.dtToINNER JOIN ( SELECT EmpID, MAX(dt) AS maxdt FROM @Sample GROUP BY EmpID ) AS y ON y.EmpID = q.EmpIDWHERE q.dt < y.maxdt AND s.EmpID IS NULL[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-25 : 11:59:48
|
[code]SELECT s.EmpID, s.dtFROM @Sample AS sWHERE NOT EXISTS (SELECT * FROM @Sample AS x WHERE x.EmpID = s.EmpID AND (x.dt BETWEEN s.dt + 1 AND s.dt + 10 OR x.dt BETWEEN s.dt - 10 AND s.dt - 1))[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
Sleepless
Starting Member
9 Posts |
Posted - 2008-03-26 : 09:47:42
|
More than one way to skin a cat. Thanks all!! |
 |
|
rdavidson
Starting Member
1 Post |
Posted - 2011-06-14 : 15:29:16
|
Sorry to bring up this old topic again, but I am trying to the exact same thing. With these examples I have been able to successfully reproduce a similar query, however I require the count of consucetive days in the results set. My query is built almost exactly like ee_rlee's reply just a little more complicated. So using his reply how would one get the count of consecutive days in the results set? Is this even possible without a cursor?SELECT *FROM Employees EWHERE EXISTS(SELECT 1FROM Absents AWHERE A.EmpID=E.EmpID AND (SELECT COUNT(*) FROM Absents WHERE EmpID=A.EmpID AND SeqID Between A.SeqID AND A.SeqID+9)>=10)If you wish to see specific information about my exact query please see this thread: http://forums.asp.net/t/1689502.aspx/1?Return+consecutive+count BTW- I'm actually on SQL 2008, just didnt think there was a need to start a new thread. |
 |
|
|
|
|
|
|