Calculating Averages Using Clock Times

Calculating Averages Using Clock Times

Question:
Joe Celko’s Store Survey Puzzle]

You are collecting statistical information stored by the quarter hour. What your customer wants is to get information by hour ? not on the hour. That is, we don’t want to know what the load was at 00:00 Hrs, at 01:00 Hrs, at 02:00 Hrs, and so forth. We want the average load for the first four quarter hours (00:00, 00:15, 00:30, 01:00), for the next four quarter hours (00:15, 00:30, 01:00, 01:15) and so forth. The sample table looks like this:

    CREATE TABLE LoadData        (calendar DATE NOT NULL,         clock TIME NOT NULL,         load REAL NOT NULL,        PRIMARY KEY(calendar, clock));

Answer:
The best way is to add another column to hold the reporting period:

    CREATE TABLE LoadData        (calendar DATE NOT NULL,         clock TIME NOT NULL,         period INTEGER DEFAULT (0) CHECK (period BETWEEN 0 AND 96),        load REAL DEFAULT (0) NOT NULL,        PRIMARY KEY (calendar, clock));
Then update the table with a series of statements like this:
    UPDATE LoadData        SET period = 0        WHERE clock IN (00:00, 00:15, 00:30, 01:00);
Now the report becomes:
    SELECT period, MAX(clock), AVG(load)        FROM LoadData        GROUP BY period;
If you are not sure that the samples will fall exactly on the quarter hour then hedge your bets a little bit by letting the clock times fall within a time frame.
    UPDATE LoadData        SET period = 1        WHERE clock BETWEEN 00:00 AND 01:00;
Just how you want to handle the query is up to you. The first query will still work, but it does not allow for the spacing of the samples should they be too close together.

Puzzle provided courtesy of:
Joe Celko
[email protected]

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes