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

  • SEARCH
  • Home
  • 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 6323513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:31:57+00:00 2026-05-24T16:31:57+00:00

I have the following data being returned from a query. Essentially I am putting

  • 0

I have the following data being returned from a query. Essentially I am putting this in a temp table so it is now in a temp table that I can query off of(Obviously a lot more data in real life, I am just showing an example):

EmpId      Date
1        2011-01-01
1        2011-01-02
1        2011-01-03
2        2011-02-03
3        2011-03-01
4        2011-03-02
5        2011-01-02

I need to return only EmpId’s that have 30 or more consecutive days in the date column. I also need to return the day count for these employees that have 30 or more consecutive days. There could potentially be 2 or more sets of different consecutive days that are 30 or more days. iIn this instance I would like to return multiple rows. So if an employee has a date from 2011-01-01 to 2011-02-20 then return this and the count in one row. Then if this same employee has dates of 2011-05-01 to 2011-07-01 then return this in another row. Essentially all breaks in consecutive days are treated as a seperate record.

  • 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-24T16:31:58+00:00Added an answer on May 24, 2026 at 4:31 pm

    Using DENSE_RANK should do the trick:

    ;WITH sampledata
        AS (SELECT 1 AS id, DATEADD(day, -0, GETDATE())AS somedate
            UNION ALL SELECT 1, DATEADD(day, -1, GETDATE())
            UNION ALL SELECT 1, DATEADD(day, -2, GETDATE())
            UNION ALL SELECT 1, DATEADD(day, -3, GETDATE())
            UNION ALL SELECT 1, DATEADD(day, -4, GETDATE())
            UNION ALL SELECT 1, DATEADD(day, -5, GETDATE())
            UNION ALL SELECT 1, DATEADD(day, -10, GETDATE())
            UNION ALL SELECT 1, '2011-01-01 00:00:00'
            UNION ALL SELECT 1, '2010-12-31 00:00:00'
            UNION ALL SELECT 1, '2011-02-01 00:00:00'
            UNION ALL SELECT 1, DATEADD(day, -10, GETDATE())
            UNION ALL SELECT 2, DATEADD(day, 0, GETDATE())
            UNION ALL SELECT 2, DATEADD(day, -1, GETDATE())
            UNION ALL SELECT 2, DATEADD(day, -2, GETDATE())
            UNION ALL SELECT 2, DATEADD(day, -6, GETDATE())
            UNION ALL SELECT 3, DATEADD(day, 0, GETDATE())
            UNION ALL SELECT 4, DATEADD(day, 0, GETDATE())
            UNION ALL SELECT 5, DATEADD(day, 0, GETDATE()))
       , ranking
        AS (SELECT *, DENSE_RANK()OVER(PARTITION BY id ORDER BY DATEDIFF(day, 0, somedate)) - DATEDIFF(day, 0, somedate)AS dategroup
              FROM sampledata)
        SELECT id
             , MIN(somedate)AS range_start
             , MAX(somedate)AS range_end
             , DATEDIFF(day, MIN(somedate), MAX(somedate)) + 1 AS consecutive_days
          FROM ranking
         GROUP BY id, dategroup
         --HAVING DATEDIFF(day, MIN(somedate), MAX(somedate)) + 1 >= 30 --change as needed
         ORDER BY id, range_start
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data being returned from my database query: +---------------+-----------+------------------+--------------+-------+ | district_name
I have date data being returned from a server in the following format through
A] Problem summary: I have JSON data being returned from python to javascript. I
I have the following JSON data being returned by a webservice: {d:[ {RelationshipManager:{},AdvisoryId:13,ClientId:22,UserId:13,AdvisoryMessage:Welcome!,Seen:false,Issued:\/Date(1312123970110)\/,Delivered:null,Importance:3}, {RelationshipManager:{},AdvisoryId:14,ClientId:22,UserId:13,AdvisoryMessage:A
I have the following script which displays data returned from a serverside page onto
I have the following JSON data being returned, but for some reason, Javascript or
I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
Hi I have following data in the table: ID-----startDate----endDate 5549 2008-05-01 4712-12-31 5567 2008-04-17
I have the following data set that I am trying to plot with ggplot2,
I have a sql table which have the following data, Id City Country ---

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.