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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:34:43+00:00 2026-06-10T07:34:43+00:00

Assuming a table such as: UID Name Datetime Users 4 Room 4 2012-08-03 14:00:00

  • 0

Assuming a table such as:

UID     Name        Datetime                Users
4       Room 4      2012-08-03 14:00:00     3
2       Room 2      2012-08-03 14:00:00     3
3       Room 3      2012-08-03 14:00:00     1
1       Room 1      2012-08-03 14:00:00     2

3       Room 3      2012-08-03 14:15:00     1
2       Room 2      2012-08-03 14:15:00     4
1       Room 1      2012-08-03 14:15:00     3

1       Room 1      2012-08-03 14:30:00     6

1       Room 1      2012-08-03 14:45:00     3
2       Room 2      2012-08-03 14:45:00     7
3       Room 3      2012-08-03 14:45:00     8
4       Room 4      2012-08-03 14:45:00     4

I wanted to get the average user count of each room (1,2,3,4) from the time 2PM to 3PM. The problem is that sometimes the room may not “check in” at the 15 minute interval time, so the assumption has to be made that the previous last known user count is still valid.

For example the check-in’s for 2012-08-03 14:15:00 room 4 never checked in, so it must be assumed that room 4 had 3 users at 2012-08-03 14:15:00 because that is what it had at 2012-08-03 14:00:00

This follows on through so that the average user count I am looking for is as follows:

Room 1: (2 + 3 + 6 + 3) / 4 = 3.5
Room 2: (3 + 4 + 4 + 7) / 4 = 4.5
Room 3: (1 + 1 + 1 + 8) / 4 = 2.75
Room 4: (3 + 3 + 3 + 4) / 4 = 3.25

where # is the assumed number based on the previous known check-in.

I am wondering if it’s possible to so this with SQL alone? if not I am curious of a ingenious PHP solution that isn’t just bruteforce math, as such as my quick inaccurate pseudo code:

foreach ($rooms_id_array as $room_id) {
    $SQL = "SELECT * FROM `table` WHERE (`UID` == $room_id && `Datetime` >= 2012-08-03 14:00:00 && `Datetime` <= 2012-08-03 15:00:00)";
    $result = query($SQL);
    if ( count($result) < 4 ) {
        // go through each date and find what is missing, and then go to previous date and use that instead
    } else {
        foreach ($result)
            $sum += $result;
        $avg = $sum / 4;
    }

}
  • 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-10T07:34:45+00:00Added an answer on June 10, 2026 at 7:34 am

    You can use this solution:

    SELECT   b.Name, 
             AVG(b.Users) avg_users
    FROM     (
             SELECT     a.UID, 
                        MAX(c.Datetime) last_date
             FROM       (SELECT DISTINCT UID FROM tbl) a
             CROSS JOIN (
                        SELECT '14:00:00' intrvl UNION ALL
                        SELECT '14:15:00'        UNION ALL
                        SELECT '14:30:00'        UNION ALL
                        SELECT '14:45:00'
                        ) b
             JOIN       tbl c ON a.UID           = c.UID
                             AND TIME(b.intrvl) >= TIME(c.Datetime)
             GROUP BY   a.UID,
                        b.intrvl
             ) a
    JOIN     tbl b ON a.UID       = b.UID
                  AND a.last_date = b.Datetime
    GROUP BY b.UID,
             b.Name
    

    Query Breakdown:


    Step 1:

    The first thing we need to do is associate each room with each time-interval. For example, in your example data, Room 4 does not have an association with intervals 14:15:00 and 14:30:00, but we still need to somehow represent those associations.

    We accomplish this by creating a Cartesian product of each distinct room with the relevant time-intervals:

    SELECT     a.UID, 
               b.intrvl
    FROM       (SELECT DISTINCT UID FROM tbl) a
    CROSS JOIN (
               SELECT '14:00:00' intrvl UNION ALL
               SELECT '14:15:00'        UNION ALL
               SELECT '14:30:00'        UNION ALL
               SELECT '14:45:00'
               ) b
    ORDER BY   b.intrvl, a.UID DESC --Ordering for display purposes
    

    Renders:

    UID | intrvl
    --------------
    4   | 14:00:00
    3   | 14:00:00
    2   | 14:00:00
    1   | 14:00:00
    4   | 14:15:00
    3   | 14:15:00
    2   | 14:15:00
    1   | 14:15:00
    4   | 14:30:00
    3   | 14:30:00
    2   | 14:30:00
    1   | 14:30:00
    4   | 14:45:00
    3   | 14:45:00
    2   | 14:45:00
    1   | 14:45:00
    

    SQLFiddle Demo


    Step 2:

    Then once we have those associations, we join the result back onto the main table (tbl) on the condition that the main table’s time part of its Datetime field is less than the Cartesian-joined time for each UID. What this will do is for each UID -> intrvl association, it will show all entries that have occurred on or before the intrvl time.

    So for example, since Room 3 doesn’t have an entry for the 14:30:00 intrvl, only two entries will join with that intrvl: the ones on 14:15:00 and 14:00:00 since they both occurred either on or before the intrvl time.

    You can now see where we are going with this. The result of this step will give us access to the most recent entry for each intrvl.

    SELECT     a.UID, 
               b.intrvl,
               c.*
    FROM       (SELECT DISTINCT UID FROM tbl) a
    CROSS JOIN (
               SELECT '14:00:00' intrvl UNION ALL
               SELECT '14:15:00'        UNION ALL
               SELECT '14:30:00'        UNION ALL
               SELECT '14:45:00'
               ) b
    JOIN       tbl c ON a.UID           = c.UID
                    AND TIME(b.intrvl) >= TIME(c.Datetime)
    ORDER BY   b.intrvl, a.UID DESC, c.Datetime --Ordering for display purposes
    

    Renders (excluding the Name column):

    UID |  intrvl    |  Datetime             |  Users
    ---------------- --------------------------------
    4   |  14:00:00  |  2012-08-03 14:00:00  |  3   <-- Most recent entry up until 14:00:00
    3   |  14:00:00  |  2012-08-03 14:00:00  |  1   <-- Most recent entry up until 14:00:00
    2   |  14:00:00  |  2012-08-03 14:00:00  |  3   <-- Most recent entry up until 14:00:00
    1   |  14:00:00  |  2012-08-03 14:00:00  |  2   <-- Most recent entry up until 14:00:00
    4   |  14:15:00  |  2012-08-03 14:00:00  |  3   <-- Most recent entry up until 14:15:00
    3   |  14:15:00  |  2012-08-03 14:00:00  |  1
    3   |  14:15:00  |  2012-08-03 14:15:00  |  1   <-- Most recent entry up until 14:15:00
    2   |  14:15:00  |  2012-08-03 14:00:00  |  3
    2   |  14:15:00  |  2012-08-03 14:15:00  |  4   <-- Most recent entry up until 14:15:00
    1   |  14:15:00  |  2012-08-03 14:00:00  |  2
    1   |  14:15:00  |  2012-08-03 14:15:00  |  3   <-- Most recent entry up until 14:15:00
    4   |  14:30:00  |  2012-08-03 14:00:00  |  3   <-- Most recent entry up until 14:30:00
    3   |  14:30:00  |  2012-08-03 14:00:00  |  1   
    3   |  14:30:00  |  2012-08-03 14:15:00  |  1   <-- Most recent entry up until 14:30:00
    2   |  14:30:00  |  2012-08-03 14:00:00  |  3
    2   |  14:30:00  |  2012-08-03 14:15:00  |  4   <-- Most recent entry up until 14:30:00
    1   |  14:30:00  |  2012-08-03 14:00:00  |  2
    1   |  14:30:00  |  2012-08-03 14:15:00  |  3
    1   |  14:30:00  |  2012-08-03 14:30:00  |  6   <-- Most recent entry up until 14:30:00
    4   |  14:45:00  |  2012-08-03 14:00:00  |  3
    4   |  14:45:00  |  2012-08-03 14:45:00  |  4   <-- Most recent entry up until 14:45:00
    3   |  14:45:00  |  2012-08-03 14:00:00  |  1
    3   |  14:45:00  |  2012-08-03 14:15:00  |  1
    3   |  14:45:00  |  2012-08-03 14:45:00  |  8   <-- Most recent entry up until 14:45:00
    2   |  14:45:00  |  2012-08-03 14:00:00  |  3
    2   |  14:45:00  |  2012-08-03 14:15:00  |  4
    2   |  14:45:00  |  2012-08-03 14:45:00  |  7   <-- Most recent entry up until 14:45:00
    1   |  14:45:00  |  2012-08-03 14:00:00  |  2
    1   |  14:45:00  |  2012-08-03 14:15:00  |  3
    1   |  14:45:00  |  2012-08-03 14:30:00  |  6
    1   |  14:45:00  |  2012-08-03 14:45:00  |  3   <-- Most recent entry up until 14:45:00
    

    SQLFiddle Demo


    Step 3:

    Our next step is to take the result-set above and pull only the most recent joined Datetime for each intrvl. We can accomplish this by using GROUP BY in conjunction with the MAX() aggregate function.

    Unfortunately, we can’t also correctly pull the value of Users along with each of the selected Datetimes due to how GROUP BY behaves.

    SELECT     a.UID, 
               b.intrvl,
               MAX(c.Datetime) last_date
    FROM       (SELECT DISTINCT UID FROM tbl) a
    CROSS JOIN (
               SELECT '14:00:00' intrvl UNION ALL
               SELECT '14:15:00'        UNION ALL
               SELECT '14:30:00'        UNION ALL
               SELECT '14:45:00'
               ) b
    JOIN       tbl c ON a.UID           = c.UID
                    AND TIME(b.intrvl) >= TIME(c.Datetime)
    GROUP BY   a.UID,
               b.intrvl
    ORDER BY   b.intrvl, a.UID DESC --Again, for display purposes
    

    Renders:

    UID |  intrvl    |  last_date
    ---------------------------------------
    4   |  14:00:00  |  2012-08-03 14:00:00
    3   |  14:00:00  |  2012-08-03 14:00:00
    2   |  14:00:00  |  2012-08-03 14:00:00
    1   |  14:00:00  |  2012-08-03 14:00:00
    4   |  14:15:00  |  2012-08-03 14:00:00
    3   |  14:15:00  |  2012-08-03 14:15:00
    2   |  14:15:00  |  2012-08-03 14:15:00
    1   |  14:15:00  |  2012-08-03 14:15:00
    4   |  14:30:00  |  2012-08-03 14:00:00
    3   |  14:30:00  |  2012-08-03 14:15:00
    2   |  14:30:00  |  2012-08-03 14:15:00
    1   |  14:30:00  |  2012-08-03 14:30:00
    4   |  14:45:00  |  2012-08-03 14:45:00
    3   |  14:45:00  |  2012-08-03 14:45:00
    2   |  14:45:00  |  2012-08-03 14:45:00
    1   |  14:45:00  |  2012-08-03 14:45:00
    

    SQLFiddle Demo


    Step 4

    Now we have to grab the value of Users for each last_date so we can take the average of those values. We do this by wrapping our query in the last step as a subselect inside the FROM clause and joining once again back onto the main table on the condition that for each matching UID -> last_date association, grab the value of Users.

    SELECT   a.UID,
             a.last_date,
             b.Users
    FROM     (
             SELECT     a.UID, 
                        MAX(c.Datetime) last_date
             FROM       (SELECT DISTINCT UID FROM tbl) a
             CROSS JOIN (
                        SELECT '14:00:00' intrvl UNION ALL
                        SELECT '14:15:00'        UNION ALL
                        SELECT '14:30:00'        UNION ALL
                        SELECT '14:45:00'
                        ) b
             JOIN       tbl c ON a.UID           = c.UID
                             AND TIME(b.intrvl) >= TIME(c.Datetime)
             GROUP BY   a.UID,
                        b.intrvl
             ) a
    JOIN     tbl b ON a.UID       = b.UID
                  AND a.last_date = b.Datetime
    ORDER BY a.UID DESC --Display purposes again
    

    Renders:

    UID | last_date           | Users
    ---------------------------------
    4   | 2012-08-03 14:00:00 | 3
    4   | 2012-08-03 14:00:00 | 3
    4   | 2012-08-03 14:00:00 | 3
    4   | 2012-08-03 14:45:00 | 4
    3   | 2012-08-03 14:00:00 | 1
    3   | 2012-08-03 14:15:00 | 1
    3   | 2012-08-03 14:15:00 | 1
    3   | 2012-08-03 14:45:00 | 8
    2   | 2012-08-03 14:00:00 | 3
    2   | 2012-08-03 14:15:00 | 4
    2   | 2012-08-03 14:15:00 | 4
    2   | 2012-08-03 14:45:00 | 7
    1   | 2012-08-03 14:00:00 | 2
    1   | 2012-08-03 14:15:00 | 3
    1   | 2012-08-03 14:30:00 | 6
    1   | 2012-08-03 14:45:00 | 3
    

    SQLFiddle Demo


    Step 5

    Now it’s just a simple matter of grouping on each room and averaging the Users column:

    SELECT   b.Name, 
             AVG(b.Users) avg_users
    FROM     (
             SELECT     a.UID, 
                        MAX(c.Datetime) last_date
             FROM       (SELECT DISTINCT UID FROM tbl) a
             CROSS JOIN (
                        SELECT '14:00:00' intrvl UNION ALL
                        SELECT '14:15:00'        UNION ALL
                        SELECT '14:30:00'        UNION ALL
                        SELECT '14:45:00'
                        ) b
             JOIN       tbl c ON a.UID           = c.UID
                             AND TIME(b.intrvl) >= TIME(c.Datetime)
             GROUP BY   a.UID,
                        b.intrvl
             ) a
    JOIN     tbl b ON a.UID       = b.UID
                  AND a.last_date = b.Datetime
    GROUP BY b.UID,
             b.Name
    

    Renders:

    Name   | avg_users
    ------------------
    Room 1 | 3.5
    Room 2 | 4.5
    Room 3 | 2.75
    Room 4 | 3.25
    

    SQLFiddle Demo of Final Result

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

Sidebar

Related Questions

Assuming I have a table as such [StockBarcodeID] [uniqueidentifier] NOT NULL, [UserID] [uniqueidentifier] NOT
Assuming an InnoDB table, will the following command ... CHECK TABLE table_name; ... cause
Assuming all my Gradle plugin user going to have a MYAPP_HOME sys variable set
Assuming I have the following schema table A: ID int primary key value varchar(255)
My MysQL database contains multiple MyISAM tables, with each table containing millions of rows.
Assuming you have the following database table: create table Names ( Id INT IDENTITY
Assuming I have an employee table with 2 columns only: employee_id manager_id All employees
Assuming that a table contains sufficient information to warrant an index seek, at what
I have a table like : trans is the name of the table for
Given a table structure like such: DECLARE @Employees TABLE(EmployeeID INT ,EmployeeDetails XML) INSERT 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.