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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:44:24+00:00 2026-05-16T15:44:24+00:00

Suppose I have this data: Employee Task IsCurrent ——– —- ——— Jeff 1 Yes

  • 0

Suppose I have this data:

Employee  Task   IsCurrent
--------  ----   ---------
Jeff      1      Yes
Jeff      2      No
Jane      1      No
Jane      2      Yes
Jane      3      No
Jane      4      No
Jake      1      Yes

Employees have some number of tasks, and one of them will be current. The task number indicates an ordering — they perform tasks in order, looping back to the beginning when they’re done with the last one. I’d like a query to see the next three tasks. I’d like this result set:

Employee    CurrentTask  NextTask  NextTask2
--------    -----------  --------  ---------
Jeff        1            2         1
Jane        2            3         4
Jake        1            1         1

Is this possible? I’m using SQL 2005

UPDATE: Sorry, I didn’t make clear that the task numbers aren’t necessarily sequental — i.e. Mike might actually have tasks 10, 23, and 223. So I can’t just join looking for the next sequential task number.

  • 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-16T15:44:25+00:00Added an answer on May 16, 2026 at 3:44 pm

    Use:

    WITH summary AS (
       SELECT t.employee,
              t.task,
              t.iscurrent,
              ROW_NUMBER() OVER (PARTITION BY t.employee
                                     ORDER BY t.task) AS rank
         FROM TABLE t)
       SELECT a.employee,
              a.task AS current_task,
              COALESCE(b.task, y.min_task) AS next_task,
              COALESCE(c.task, y.min_task) AS next_task2
         FROM summary a
         JOIN (SELECT x.employee,
                      MIN(x.task) AS min_task
                 FROM TABLE x
             GROUP BY x.employee) y ON y.employee = a.employee
    LEFT JOIN summary b ON b.employee = a.employee
                       AND b.rank = a.rank + 1
    LEFT JOIN summary c ON c.employee = a.employee
                       AND c.rank = a.rank + 2                   
        WHERE a.iscurrent = 'yes'
    

    Like I said earlier, would be easier if SQL Server supported LEAD/LAG functions…

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

Sidebar

Related Questions

Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
-- This is an updated post, old post removed -- Suppose I have data
Let's suppose I have a variable called data. This data variable has all this
Suppose that I have a class like this class Employee: pass I create two
Suppose I have this class: class MyClass(object): def uiFunc(self, MainWindow): self.attr1 = foo self.attr2
Suppose I have this table parent | child 1 2 1 3 2 4
Suppose I have this table: id | name | city ------------------ 1 | n1
Suppose I have this (C++ or maybe C) code: vector<int> my_vector; for (int i
Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func());
Suppose I have this feature branch foo. Now I want to merge it back

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.