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 9211183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:14:15+00:00 2026-06-18T01:14:15+00:00

I have a table has data similar to below Emp Date Code — ——–

  • 0

I have a table has data similar to below

  Emp  Date        Code     
  ---  --------    ---- 
  E1  11/1/2012    W 
  E1  11/1/2012    V   
  E2  11/1/2012    W   
  E1  11/2/2012    W
  E1  11/3/2012    W
  E1  11/4/2012    W
  E1  11/5/2012    W

I want to get list of employees between a date range(say for the last 3 months) who worked for code W conescutively for 5 days with the date range in the output. Each employee can have multiple records for a single day with different codes.

Expected Output is

Emp   Date-Range 
---   ----------
 E1   11/1 -11/5

Below is what I tried but I didn’t come close at all to the output I seek

 SELECT distinct user, MIN(date) startdate, MAX(date) enddate
FROM (SELECT user, date, (TRUNC(date) - ROWNUM) tmpcol
      FROM (SELECT user, date
              FROM tablename
             where date between to_date('10/01/2012','mm/dd/yyyy') and to_date('10/03/2012','mm/dd/yyyy')
             ORDER BY user, date) ot) t
 GROUP BY user, tmpcol
 ORDER BY user, startdate;

If Emp E1 has worked for 10 consecutive days, he should be listed twice in the output with both date ranges. If E1 has worked for 9 days consecutively(11/1 to 11/9), he should be listed only once with the date range 11/1 to 11/9.

I have already seen questions which are similar but none of them exactly worked out for me. My database is Oracle 10G and no PL/SQL.

  • 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-06-18T01:14:17+00:00Added an answer on June 18, 2026 at 1:14 am

    I’m not sure I understood everything correctly, but something like this might get you started:

    select emp, 
           sum(diff) as days,
           to_char(min(workdate), 'yyyy-mm-dd') as work_start,
           to_char(max(workdate), 'yyyy-mm-dd') as work_end
    from (       
      select *
      from (
        select emp, 
               workdate, 
               code, 
               nvl(workdate - lag(workdate) over (partition by emp, code order by workdate),1) as diff
        from tablename
        where code = 'W'
         and workdate between ...
      ) t1
      where diff = 1 -- only consecutive rows
    ) t2
    group by emp
    having sum(diff) = 5
    

    SQLFiddle: http://sqlfiddle.com/#!4/ad7ae/3

    Note that I used workdate instead of the date as it is a bad idea to use a reserved word as a column name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that has data like below(sorted by date) COL_A | COL_B
I have a table with 692256 number of rows ,which has data similar to
I have a table (call it table myTable). It has the data like this:
I have a table, with cells, and each cell has a data-type. I have
I have a table that stores data that has been entered regarding the amount
I have a table of data which has posts, then I have a separate
I have an access table which has some cells as blank ( no data
I have a data table which already has some values, plus it is getting
I have a sql table of payroll data that has wage rates and effective
I have a table in my data base that has three columns: Screen, Icon,

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.