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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:52:39+00:00 2026-06-15T05:52:39+00:00

I’ve met an interesting problem. I have a table of workers’ ids’ and days

  • 0

I’ve met an interesting problem.
I have a table of workers’ ids’ and days of their visits. Here is dump:

CREATE TABLE `pp` (
  `id` int(11) DEFAULT '1',
  `day` int(11) DEFAULT '1',
  `key` varchar(45) NOT NULL,
 PRIMARY KEY (`key`)
) 

INSERT INTO `pp` VALUES 
   (1,1,'1'),
   (1,20,'2'),
   (1,50,'3'),
   (1,70,'4'),
   (2,1,'5'),
   (2,120,'6'),
   (2,90,'7'),
   (1,90,'8'),
   (2,100,'9');

So I need to find workers which have missed more than 50 days at least once. For example, if worker visited at 5th, 95th, 96th, 97th day, if we look at deltas, we can see that the largest delta (90) is more than 50, so we should include this worker into result.
The problem is how do I efficiently find deltas between visits of different workers?

I can’t even imagine how to work with mysql tables as consequent arrays of data.

So we need to separate day values for different workers, sort them and then find max deltas for each. But how? Is there any way to, for example, enumerate sorted arrays in 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-15T05:52:41+00:00Added an answer on June 15, 2026 at 5:52 am

    This is a way I used to cope with such problems:

    SELECT distinct t3.id  FROM
    (SELECT t1.id, t1.day, MIN(t2.day) nextday
    FROM pp t1
    JOIN pp t2 ON t1.id=t2.id AND t1.day<t2.day
    GROUP BY t1.id, t1.day
    HAVING nextday-t1.day >50) t3
    

    (EDIT this version is slightly better)
    This finds all the IDs for which there is a delta > 50. (I assumed that this is what you’re after)

    To see it working: SQL fiddle

    To find the max deltas:

    SELECT t3.id, MAX(t3.nextday-t3.day)  FROM
    (SELECT t1.id, t1.day, MIN(t2.day) nextday
    FROM pp t1
    JOIN pp t2 ON t1.id=t2.id AND t1.day<t2.day
    GROUP BY t1.id, t1.day) t3
    GROUP BY t3.id
    

    The logic behind is to find the “next” item, whatever that means. As this is an ordered attribute, the next item can be defined as having the lowest value among those rows that have the value larger than the one examined… Then you join the “next” values to the original values, conpute the delta, and return only those that are applicable. If you need the other columns too, just do a JOIN on the outer select to the original table.

    I’m not sure if this is the best solution regarding perfirmance, but I only wrote queries for one-off reports, with which I could afford the query to run for a while.

    There is one semantic error though, that can arise: if somebody was present on the 1st, 2nd and 3rd days, but never after, this does not find the absence. To overcome this, you could add a special row with UNIONing a select to the tables specifying tomorrow’s day count for all IDs, but that would make this query disgusting enough not to try writing it down…

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have been unable to fix a problem with Java Unicode and encoding. The
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I am currently running into a problem where an element is coming back from
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.