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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:42:35+00:00 2026-06-09T18:42:35+00:00

I have a table, employee history with the following ID | Current_Dept | Dept_Start_Date

  • 0

I have a table, employee history with the following

ID | Current_Dept | Dept_Start_Date | Name
1 | Sales | 2012-01-01 | John Smith
1 | Marketing | 2010-01-01 | John Smith
1 | Intern | 2008-01-01 | John Smith
2 | IT | 2012-01-01 | Sue Jones
2 | Sales | 2011-01-01 | Sue Jones
2 | eBusiness | 2010-10-01 | Sue Jones
3 | Warehouse | 2012-01-01 | Bobby Ray
3 | Sales | 2009-01-01 | Bobby Ray

What I would like is a query giving everyone who ever worked in Sales:

ID | DeptBefore | DeptAfter | CurrentDept
1  | Marketing  |           | Sales
2  | ebusiness  | IT        | IT
3  |            | Warehouse | Warehouse

At the moment I see no simpler way of doing this other then recursively querying the table for each row.

I’m tempted to create a ‘temp’ table that’s populated once a day and run the query off of that.

I’m using SQL Server and C# (ASP.NET) incase these offer other simpler solutions.

I hope that all makes sense.

Thanks,

  • 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-09T18:42:36+00:00Added an answer on June 9, 2026 at 6:42 pm

    From your data it appears that you want the departments immediately before and after the sales department. For example; you don’t show that John Smith started as an intern.

    NOTE: You may want to consider the results you want if someone has worked for sales on two different occasions.

    WITH
      sequenced AS
    (
      SELECT
        *,
        ROW_NUMBER() OVER (PARTITION BY id ORDER BY dept_start_date DESC) AS sequence_id
      FROM
        yourTable
    )
    SELECT
      salesRecord.id,
      prevRecord.current_dept     AS DeptBefore,
      nextRecord.current_dept     AS DeptAfter,
      lastRecord.current_dept     AS DeptCurrent
    FROM
      sequenced     AS salesRecord
    LEFT JOIN
      sequenced     AS prevRecord
        ON  prevRecord.id          = salesRecord.id
        AND prevRecord.sequence_id = salesRecord.sequence_id - 1
    LEFT JOIN
      sequenced     AS nextRecord
        ON  nextRecord.id          = salesRecord.id
        AND nextRecord.sequence_id = salesRecord.sequence_id + 1
    LEFT JOIN
      sequenced     AS lastRecord
        ON  lastRecord.id          = salesRecord.id
        AND lastRecord.sequence_id = 1
    WHERE
      salesRecord.CurrentDept = 'Sales'
    

    By ordering the items in Descending order, the current record is always sequence_id = 1.

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

Sidebar

Related Questions

Let's say I have the following table: Employee name start_date end_date John 2009-10-10 2009-12-31
I have a table called Employee with the following fields: EmpID Salary Name I
I have table with following details Table name EMPLOYEE and columns EMPID (PK smallint
Let's suppose that I have the following table: employee (id, name, surname, salary); The
I have an employee table, with the following data. For a particular manager, i
I have a table which has employee relationship defined within itself. i.e. EmpID Name
Well I have this - Table DimDate- Date Table Employee- Id,Name,Points,Date Now the Employee
I have a table like Employee ================== name salary ================== a 10000 b 20000
Say I have a table called Employee (has ID , NAME , ADDRESS ,
I have an employee and a corresponding employee history table. Both the tables have

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.