Calculating Averages Within Arbitrary Groups

Calculating Averages Within Arbitrary Groups

Question:
[Joe Celko’s Production Report Puzzle]

We get a production report from work centers which has a date, center code and how many widgets were produced from each batch of raw materials sent to them that day. It looks like this:

    CREATE TABLE Production        (center INTEGER NOT NULL,        wkdate DATE NOT NULL,        batchno INTEGER NOT NULL,        widgets INTEGER NOT NULL,        PRIMARY KEY (center, wkdate, batchno));
The boss comes in and says he wants to know the average number of widgets Produced by date and center. We say no problem and do it. The next day he comes back and wants the same data in groups of three. In other words, if on 1994 Feb 24 in center 42, we processed nine batches, the report will show the average number of widgets made from the first three batches, the second three batches and the last three batches.

Answer:
The first query is very straightforward:

    SELECT center, wkdate, COUNT(batchno), AVG(widgets)        FROM Production        GROUP BY center, wkdate;
We have to make some assumptions about the second query and decide how to handle daily work where the number of batches is not evenly divisible by three. I am throwing anyone on the border into the lower third; thus, eight batches would be in ordered thirds of 3, 3 and 2 batches each. We can assume the batches are numbered from 1 to (n) for each day’s work and make life easier.

Using the CASE expression in SQL-92, we can find which third a batchno is in with a VIEW, thus:

    CREATE VIEW Prod3 (center, wkdate, third, widgets)        AS SELECT center, wkdate,            CASE WHEN batchno <= MAX(batchno)/3 THEN 1            WHEN batchno > (2*MAX(batchno))/3 THEN 3            ELSE 2            END, widgets        FROM Production;
If you do not have this in your SQL, then you might try something like this:
    CREATE VIEW Prod3 (center, wkdate, third, batchno, widgets)        AS SELECT center, wkdate, 1, batchno, widgets            FROM Production AS P1            WHERE batchno <= (SELECT MAX(batchno)                FROM Production AS P2                WHERE P1.center = P2.center                    AND P1.wkdate = P2.wkdate)        UNION        SELECT center, wkdate, 2, batchno, widgets            FROM Production AS P1            WHERE batchno > (SELECT MAX(batchno)                FROM Production AS P2                WHERE P1.center = P2.center                    AND P1.wkdate = P2.wkdate)            AND batchno <= (SELECT 2 * MAX(batchno)                FROM Production AS P2                WHERE P1.center = P2.center                    AND P1.wkdate = P2.wkdate)        UNION        SELECT center, wkdate, 3, batchno, widgets            FROM Production AS P1            WHERE batchno > (SELECT 2 * MAX(batchno)                FROM Production AS P2                WHERE P1.center = P2.center                    AND P1.wkdate = P2.wkdate);
Either way, we end up with the final query:
    SELECT center, wkdate, third, COUNT(batchno), AVG(widgets)        FROM Prod3        GROUP BY center, wkdate, third;

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