-
Notifications
You must be signed in to change notification settings - Fork 0
/
C factor.sql
46 lines (41 loc) · 1.29 KB
/
C factor.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-- MAKE SURE YOU HAVE REPORTED YOUR DAILY SCRUM MEETINGS BEFORE TRYING TO RUN THIS QUERY!
CREATE OR REPLACE FUNCTION fetch_c_value(pid INT)
RETURNS FLOAT AS $$
DECLARE
avg_score FLOAT;
BEGIN
WITH date_series AS (
-- Generating a series of dates for each milestone
SELECT m.id AS milestone_id,
generate_series(
date_trunc('day', m.estimated_start)::date,
date_trunc('day', m.estimated_finish)::date,
'1 day'::interval
) AS date
FROM milestones_milestone m
WHERE m.project_id = pid
AND (m.id) = (
SELECT "id"
FROM "public"."milestones_milestone"
WHERE "public"."milestones_milestone"."project_id" = pid
ORDER BY "estimated_finish" DESC
LIMIT 1
)
),
scored_dates AS (
-- Assigning scores based on meetings
SELECT ds.milestone_id,
ds.date,
CASE WHEN sm.meeting_date IS NOT NULL THEN 1 ELSE 0 END AS score
FROM date_series ds
LEFT JOIN scrum_meetings sm ON ds.date = sm.meeting_date AND sm.project = pid
)
-- Calculating the average score for each milestone
SELECT AVG(score)
INTO avg_score
FROM scored_dates
GROUP BY milestone_id
ORDER BY milestone_id;
RETURN avg_score;
END;
$$ LANGUAGE plpgsql;