Log in  ::  Register  ::  Not logged in

consecutive days count irrespective of weekends, holidays

 Author Message bunnie SSC Journeyman Group: General Forum Members Points: 94 Visits: 66 Hello,I am working with SQL 2008r2. I have a situation where I need to count >15 consecutive school days absent by a student,irrespective of weekends, holidays.The attendance table is having one record for a student for each school day( weekends,HOLIDAYS not included in this table)--------------Table structure :Studentid DateID attendaceind1234 20121031 11234 20121102 11234 20121101 11234 20121105 01234 20121107 11234 20121108 11234 20121109 01234 20121113 01234 20121115 01234 20121114 01234 20121116 01234 20121119 01234 20121120 01234 20121126 01234 20121127 01234 20121128 01234 20121129 01234 20121130 01234 20121203 01234 20121204 01234 20121205 01234 20121206 01234 20121207 01234 20121210 01234 20121211 01234 20121212 01234 20121213 01234 20121214 11234 20121217 11234 20121218 11234 20121219 1-------------------1= Present ; 0 = absentNeed to count 20 consecutive school days absent.i tried to group the days absent. this is what I getStudentid startdate enddate daysabsent day week1234 20120904 20120907 4 4 361234 20120914 20120914 1 14 371234 20121022 20121022 1 22 431234 20121105 20121105 1 5 451234 20121109 20121109 1 9 451234 20121113 20121116 4 13 461234 20121119 20121120 2 19 471234 20121126 20121130 5 26 481234 20121203 20121207 5 3 491234 20121210 20121213 4 10 501234 20130125 20130125 1 25 4Starting from 20121113 untill 20121213 , these are all consecutive school days(including weekends/holidays) But what I want is , Desired resultstudentid startdate enddate daysabsent1234 20121113 20121213 20All your help is greatly appreciated.Thanks in advance!!awaiting help !! Sean Lange SSC Guru Group: General Forum Members Points: 147471 Visits: 18564 It seems that a calendar table would help you considerably. You can read about them here. http://www.sqlservercentral.com/articles/T-SQL/70482/If you need help with the query please first read the article in my signature about best practices when posting questions. _______________________________________________________________Need help? Help us help you. Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.Need to split a string? Try Jeff Modens splitter.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2) Alan Burstein SSC-Dedicated Group: General Forum Members Points: 32201 Visits: 8572 This should do the trick: `-- SetupIF OBJECT_ID('tempdb..#x') IS NOT NULL DROP TABLE #x;CREATE TABLE #x ( Studentid int, DateID bigint unique, attendaceind bit)INSERT INTO #x SELECT 1234, 20121031, 1 UNION ALL SELECT 1234, 20121102, 1 UNION ALL SELECT 1234, 20121101, 1 UNION ALL SELECT 1234, 20121105, 0 UNION ALL SELECT 1234, 20121107, 1 UNION ALL SELECT 1234, 20121108, 1 UNION ALL SELECT 1234, 20121109, 0 UNION ALL SELECT 1234, 20121113, 0 UNION ALL SELECT 1234, 20121115, 0 UNION ALL SELECT 1234, 20121114, 0 UNION ALL SELECT 1234, 20121116, 0 UNION ALL SELECT 1234, 20121119, 0 UNION ALL SELECT 1234, 20121120, 0 UNION ALL SELECT 1234, 20121126, 0 UNION ALL SELECT 1234, 20121127, 0 UNION ALL SELECT 1234, 20121128, 0 UNION ALL SELECT 1234, 20121129, 0 UNION ALL SELECT 1234, 20121130, 0 UNION ALL SELECT 1234, 20121203, 0 UNION ALL SELECT 1234, 20121204, 0 UNION ALL SELECT 1234, 20121205, 0 UNION ALL SELECT 1234, 20121206, 0 UNION ALL SELECT 1234, 20121207, 0 UNION ALL SELECT 1234, 20121210, 0 UNION ALL SELECT 1234, 20121211, 0 UNION ALL SELECT 1234, 20121212, 0 UNION ALL SELECT 1234, 20121213, 0 UNION ALL SELECT 1234, 20121214, 1 UNION ALL SELECT 1234, 20121217, 1 UNION ALL SELECT 1234, 20121218, 1 UNION ALL SELECT 1234, 20121219, 1 GODECLARE @startdate bigint=20121113, @endDate bigint=20121213;;WITH answer AS( SELECT StudentID, COUNT(*) AS DaysAbs FROM #x WHERE DateID>=@startdate AND DateID<=@endDate AND attendaceind=0 GROUP BY Studentid)SELECT StudentID, @startdate AS startdate, @endDate AS endDate, DaysAbsFROM answerGO` -- Alan BursteinHelpful links:Best practices for getting help on SQLServerCentral -- Jeff ModenHow to Post Performance Problems -- Gail ShawNasty fast set-based string manipulation functions:For splitting strings try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL Server 2012+)To split strings based on patterns try PatternSplitCMNeed to clean or transform a string? try NGrams, PatExclude8K, PatReplace8K, DigitsOnlyEE, or Translate8KI cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. -- Itzik Ben-Gan 2001 Alan Burstein SSC-Dedicated Group: General Forum Members Points: 32201 Visits: 8572 Using the sample code I created before you can also get days in/days not in using this:`DECLARE @startdate bigint=20121101, @endDate bigint=20121130;;WITH notthere AS( SELECT StudentID, COUNT(*) AS DaysAbs FROM #x WHERE DateID>=@startdate AND DateID<=@endDate AND attendaceind=0 GROUP BY Studentid),wasthere AS( SELECT StudentID, COUNT(*) AS DaysIn FROM #x WHERE DateID>=@startdate AND DateID<=@endDate AND attendaceind=1 GROUP BY Studentid)SELECT nt.StudentID, @startdate AS startdate, @endDate AS endDate, nt.DaysAbs, wt.DaysInFROM notthere ntOUTER APPLY wasthere wt`I did this query for 11/2012 since there was a better mix of data. This will get you: `StudentID startdate endDate DaysAbs DaysIn----------- -------------------- -------------------- ----------- -----------1234 20121101 20121130 13 4` -- Alan BursteinHelpful links:Best practices for getting help on SQLServerCentral -- Jeff ModenHow to Post Performance Problems -- Gail ShawNasty fast set-based string manipulation functions:For splitting strings try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL Server 2012+)To split strings based on patterns try PatternSplitCMNeed to clean or transform a string? try NGrams, PatExclude8K, PatReplace8K, DigitsOnlyEE, or Translate8KI cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. -- Itzik Ben-Gan 2001 ScottPletcher SSC-Forever Group: General Forum Members Points: 46170 Visits: 8026 `SELECT Studentid, MIN(dateid) AS First_Absence_Date, MAX(dateid) AS Last_Absence_Date, COUNT(*) AS Total_Consecutive_AbsencesFROM ( SELECT Studentid, attendanceind, dateid, ROW_NUMBER() OVER (PARTITION BY Studentid ORDER BY dateid) - ROW_NUMBER() OVER (PARTITION BY Studentid, attendanceind ORDER BY dateid) AS Grp# FROM #x x --<<-- change to your table name) AS derivedWHERE attendanceind = 0GROUP BY Studentid, grp#HAVING COUNT(*) >= 15ORDER BY Studentid, First_Absence_Date` SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them. dwain.c SSC-Forever Group: General Forum Members Points: 43787 Visits: 6431 Using Alan.B's set up data, here's another way (except using DATE data type):`-- SetupIF OBJECT_ID('tempdb..#x') IS NOT NULL DROP TABLE #x;CREATE TABLE #x ( Studentid int, DateID DATE unique, attendaceind bit)INSERT INTO #x SELECT 1234, '20121031', 1 UNION ALL SELECT 1234, '20121102', 1 UNION ALL SELECT 1234, '20121101', 1 UNION ALL SELECT 1234, '20121105', 0 UNION ALL SELECT 1234, '20121107', 1 UNION ALL SELECT 1234, '20121108', 1 UNION ALL SELECT 1234, '20121109', 0 UNION ALL SELECT 1234, '20121113', 0 UNION ALL SELECT 1234, '20121115', 0 UNION ALL SELECT 1234, '20121114', 0 UNION ALL SELECT 1234, '20121116', 0 UNION ALL SELECT 1234, '20121119', 0 UNION ALL SELECT 1234, '20121120', 0 UNION ALL SELECT 1234, '20121126', 0 UNION ALL SELECT 1234, '20121127', 0 UNION ALL SELECT 1234, '20121128', 0 UNION ALL SELECT 1234, '20121129', 0 UNION ALL SELECT 1234, '20121130', 0 UNION ALL SELECT 1234, '20121203', 0 UNION ALL SELECT 1234, '20121204', 0 UNION ALL SELECT 1234, '20121205', 0 UNION ALL SELECT 1234, '20121206', 0 UNION ALL SELECT 1234, '20121207', 0 UNION ALL SELECT 1234, '20121210', 0 UNION ALL SELECT 1234, '20121211', 0 UNION ALL SELECT 1234, '20121212', 0 UNION ALL SELECT 1234, '20121213', 0 UNION ALL SELECT 1234, '20121214', 1 UNION ALL SELECT 1234, '20121217', 1 UNION ALL SELECT 1234, '20121218', 1 UNION ALL SELECT 1234, '20121219', 1 GODECLARE @startdate DATE='20121114', @endDate DATE='20121213';--;WITH Absences AS ( SELECT StudentID, StartDT=MIN(DateID), EndDT=MAX(DateID) ,Absences=COUNT(1-attendaceind) FROM ( SELECT StudentID, DateID, attendaceind ,n=DATEDIFF(day, DateID ,ROW_NUMBER() OVER (PARTITION BY StudentID ORDER BY DateID)) FROM #x WHERE attendaceind = 0) a GROUP BY StudentID, n-- )--SELECT StudentID, StartDT=MIN(StartDT), EndDT=MAX(EndDT)-- ,Absences=SUM(Absences)--FROM (-- SELECT StudentID, StartDT, EndDT-- ,Absences=CASE WHEN StartDT >= @startdate AND EndDT <= @enddate THEN Absences-- WHEN StartDT < @startdate AND EndDT <= @enddate -- THEN DATEDIFF(day, @startdate, EndDT)-- WHEN StartDT >= @startdate AND EndDT > @enddate -- THEN DATEDIFF(day, StartDT, @enddate)-- ELSE DATEDIFF(day, @startdate, @enddate) END-- FROM Absences-- WHERE EndDT >= @startdate AND StartDT <= @enddate) a--GROUP BY StudentIDDROP TABLE #x`The initial code provided groups consecutive absences. If you uncomment the rest of the code it then groups again within the provided start and end dates summing up the absences that occur as part of a consecutive group of absences.When you run the first set of code, notice how one record starts at 11-13? The start date range of 11-14 requires the absence count to be adjusted in the final grouping and that is done with the CASE statement.All that's left is to add a HAVING clause to filter out only records appearing that are greater whatever your threshold is.I'm not sure which of the provided solutions will work best for you but surely there's something here you can work with. My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables bunnie SSC Journeyman Group: General Forum Members Points: 94 Visits: 66 First let me appolozige for posting the code in a clumsy manner, though I tried creating spaces, it did end up like that after I posted it. Will try to be clear next time I post.Sean Lange - Thank you!Alan.B -- Thank you for both the codes, I like the one with days out & days in .ScottPletcher -- Thanks a ton!! dwain.c -- Thanks for the code, I was able to get the code till the first grouping you mentioned , the commented portion of the code is where I am stuck.Thanks for the explanation.For now I am going with Scott Pletcher's code.Thank you all once again!!

Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

Select a forum
 SQL Server 2017      SQL Server 2017 - Administration      SQL Server 2017 - Development SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 15 - Administration      SQL Server 15 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions      CosmosDB      Azure Data Lake      Azure Machine Learning Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      SQL Server Security Skills      Question of the Day (QOD) Microsoft Access      Microsoft Access Products and Books      Third Party Products         SQLCentric         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         Golden Gate Software         Idera         Lumigent         Red Gate Software         ApexSQL         Quest Software         CA      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books Notification Services      Administration Article Discussions Future Versions      SQL 12

Search

 Copyright © 2002-2018 Redgate. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.