So I have a dataset that is effectively a list of jobs. These records contain a count of the number of items in each job, and it's duration. Sample data is here if anyone wants to follow along:
Now, I'm interested in generating percentiles from this data. For example, to determine the 75th percentile (by duration) you order the set, and grab the 9th record of the 12 total. This has a duration = 9, so that's your 75th percentile.
Or in code:
But what if I want to use the job count instead? What if I want to take a running sum of the job_count and use that to determine percentiles? I want to look at the data like this:
Using the running total, I'd be looking for the duration that corresponds with 579 * 0.75, or 434. Which would be duration = 7.
Basically, I don't want jobs to be the distinct event, I want the individual job items to be considered the event, essentially weighting the jobs with higher counts.
Now, is there an easy way to do this in SQL?
Code:
CREATE TABLE test_data
(
job_id NUMBER,
job_count NUMBER,
duration NUMBER
);
INSERT INTO test_data (job_id, job_count, duration) VALUES (1, 37, 9);
INSERT INTO test_data (job_id, job_count, duration) VALUES (2, 47, 4);
INSERT INTO test_data (job_id, job_count, duration) VALUES (3, 66, 6);
INSERT INTO test_data (job_id, job_count, duration) VALUES (4, 46, 6);
INSERT INTO test_data (job_id, job_count, duration) VALUES (5, 54, 1);
INSERT INTO test_data (job_id, job_count, duration) VALUES (6, 35, 1);
INSERT INTO test_data (job_id, job_count, duration) VALUES (7, 55, 9);
INSERT INTO test_data (job_id, job_count, duration) VALUES (8, 82, 7);
INSERT INTO test_data (job_id, job_count, duration) VALUES (9, 12, 9);
INSERT INTO test_data (job_id, job_count, duration) VALUES (10, 52, 4);
INSERT INTO test_data (job_id, job_count, duration) VALUES (11, 3, 9);
INSERT INTO test_data (job_id, job_count, duration) VALUES (12, 90, 2);
Code:
JOB_ID JOB_COUNT DURATION
1 37 9
2 47 4
3 66 6
4 46 6
5 54 1
6 35 1
7 55 9
8 82 7
9 12 9
10 52 4
11 3 9
12 90 2
Now, I'm interested in generating percentiles from this data. For example, to determine the 75th percentile (by duration) you order the set, and grab the 9th record of the 12 total. This has a duration = 9, so that's your 75th percentile.
Or in code:
Code:
SELECT
PERCENTILE_DISC( 0.75 )
WITHIN GROUP (ORDER BY duration ASC)
AS third_quartile
FROM
test_data;
But what if I want to use the job count instead? What if I want to take a running sum of the job_count and use that to determine percentiles? I want to look at the data like this:
Code:
SELECT
job_id,
job_count,
SUM(
job_count
)
OVER (
ORDER BY duration
ROWS UNBOUNDED PRECEDING
)
AS running_total,
duration
FROM
test_data
ORDER BY
duration ASC;
Code:
JOB_ID JOB_COUNT RUNNING_TOTAL DURATION
6 35 35 1
5 54 89 1
12 90 179 2
2 47 226 4
10 52 278 4
3 66 344 6
4 46 390 6
8 82 472 7
9 12 484 9
1 37 521 9
11 3 524 9
7 55 579 9
Using the running total, I'd be looking for the duration that corresponds with 579 * 0.75, or 434. Which would be duration = 7.
Basically, I don't want jobs to be the distinct event, I want the individual job items to be considered the event, essentially weighting the jobs with higher counts.
Now, is there an easy way to do this in SQL?