Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6200577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:23:38+00:00 2026-05-24T04:23:38+00:00

I’m looking for a way to count the distinct periods without gaps students have

  • 0

I’m looking for a way to count the distinct periods without gaps students have been absence:

Each time a student turns in an absence I create a record in the absence table, some absences can overlap, some prolong a previous absence.

StuId    StrPer       EndPer
------   -----------  -----------
111111   2011-01-10   2011-01-15
222222   2011-02-01   2011-02-05
222222   2011-02-06   2011-02-08
333333   2011-04-07   2011-04-14
444444   2011-04-20   2011-04-25
444444   2011-04-23   2011-04-28
111111   2011-05-01   2011-05-03

Now I want to count the number of unique absence periods with gaps, the result should be:

StuId   NbrAbs
------  ------
111111  2
222222  1
333333  1
444444  1

111111 has two absence period with a gap between

222222 also has two absence periods but without gap, so it must be considered as 1 absence period

333333 has only 1 absence period

444444 has 2 periods that overlap, also without gap, so it must be considered as 1 absence period

Can anyone help me write a query for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:23:39+00:00Added an answer on May 24, 2026 at 4:23 am

    Assuming SQL 2005+ this should work:

    SELECT '111111' as stuid,'2011-01-10' as start_date,'2011-01-15' as end_date into #data UNION ALL
    SELECT '222222','2011-02-01','2011-02-05' UNION ALL
    SELECT '222222','2011-02-06','2011-02-08' UNION ALL
    SELECT '333333','2011-04-07','2011-04-14' UNION ALL
    SELECT '444444','2011-04-20','2011-04-25' UNION ALL
    SELECT '444444','2011-04-23','2011-04-28' UNION ALL
    SELECT '111111','2011-05-01','2011-05-03' 
    
    ;with periods as
    (
    select 
    stuid
    ,start_date
    ,end_date
    ,row_number() OVER (PARTITION BY stuid ORDER BY end_date ASC) as period_number
    FROM #data
    )
    ,periods2 AS
    (
    SELECT 
    p1.stuid
    ,p1.start_date
    ,p1.end_date
    ,p1.period_number
    ,ISNULL(DATEDIFF(DD,p1.end_date,p2.start_date),1) as period_gap
    from periods p1
    LEFT OUTER JOIN periods p2 on p2.stuid = p1.stuid
    AND p2.period_number = p1.period_number + 1
    )
    SELECT 
    stuid
    ,count(period_gap) as number_discrete_absences
    FROM periods2
    WHERE period_gap > 0
    GROUP BY stuid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.