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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:58:31+00:00 2026-05-13T18:58:31+00:00

I have a table columns Id and EmployeeID. The table data has the following

  • 0

I have a table columns Id and EmployeeID. The table data has the following peculiarity: in some parts (where the Id is consecutive), the same EmployeeID can sometimes be found, for example

Id | EmployeeID
---------------
1  |     1
2  |     1
3  |     2
4  |     5
5  |     1
6  |     1

I want to build a query to find blocks of data containing the same EmployeeID where the Id is consecutive (with a minimum value of x records). So far I came up with:

SELECT EmployeeID, MIN(Id), MAX(Id), COUNT(*)
FROM recs
GROUP BY EmployeeID
HAVING COUNT(*) > 5 AND
       MAX(Id) - MIN(Id) + 1 = COUNT(*)

This query will bring me some (but not all) blocks of data, as long as the same Employee can only be found in a single block. Can anyone come up with a solution which will provide all different blocks of data for each employee?

  • 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-13T18:58:31+00:00Added an answer on May 13, 2026 at 6:58 pm

    Not the best solution, but it should work (for example, 3 consecutive ids):

    SELECT Id, EmployeeID FROM
    (
    SELECT r.Id, r.EmployeeID, 
    (SELECT COUNT(1) FROM recs r1 WHERE (r1.EmployeeID = r.EmployeeID AND r1.id = r.Id-1) AS c1,
    (SELECT COUNT(1) FROM recs r2 WHERE (r2.EmployeeID = r.EmployeeID AND r2.id = r.Id-2) AS c2,
    (SELECT COUNT(1) FROM recs r3 WHERE (r3.EmployeeID = r.EmployeeID AND r3.id = r.Id-3) AS c3
    FROM recs r1) tab1
    WHERE (tab1.c1+tab1.c2+tab1.c3 =3);
    

    I suggested that Id is a primary(or a unique) key. If it’s not, you should change a little each of sub-queries to something like SELECT IF(COUNT(1) >0,1,0) …..

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

Sidebar

Related Questions

I have a table y Which has two columns a and b Entries are:
I have two entities: Project, Employee Employee has primary key {employeeId} + some other
I have table with following details Table name EMPLOYEE and columns EMPID (PK smallint
This question requires some hypothetical background. Let's consider an employee table that has columns
I have a table with columns ID, DateStamp and the ID need not be
I have a table with columns Index, Date where an Index may have multiple
I have a table with three columns: user varchar, status varchar , rep int
I have a table with four columns: PartNumber, ValvePartNumber, ActuatorPartNumber, Price I want to
I have a table with arbitrary columns and rows. This fact is irrelevant though
Let's say I have a table tbl with columns id and title . I

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.