-
Notifications
You must be signed in to change notification settings - Fork 0
/
A factor.sql
113 lines (107 loc) · 3.64 KB
/
A 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
-- MAKE SURE YOU HAVE ENTERED THE USERS' COMMITMENT BEFORE TRYING TO RUN THIS QUERY!
CREATE OR REPLACE FUNCTION fetch_a_value(pid INT)
RETURNS FLOAT AS $$
DECLARE
ratio FLOAT;
BEGIN
-- Calculating the ratio
SELECT
CASE
WHEN "storypoints"."total_SPs" = 0 OR "commitment"."expected_storypoints" = 0 THEN 0
ELSE ("storypoints"."total_SPs" / "commitment"."expected_storypoints")
END AS "res"
INTO ratio
FROM
-- team commitment
(
SELECT
CASE
WHEN "public"."users_commitment"."expected_storypoints" IS NULL THEN 0
ELSE "public"."users_commitment"."expected_storypoints"
END AS "expected_storypoints"
FROM
"public"."users_commitment"
LEFT JOIN "public"."scrum_projects" AS "Scrum Projects - Project" ON "public"."users_commitment"."project" = "Scrum Projects - Project"."ref"
WHERE
"Scrum Projects - Project"."ref" = pid
AND ("date_of_entry") >= (
SELECT
"estimated_start"
FROM
"public"."milestones_milestone"
WHERE
"public"."milestones_milestone"."project_id" = pid
ORDER BY
"estimated_finish" DESC
LIMIT
1
)
AND ("date_of_entry") < (
SELECT
"estimated_finish"
FROM
"public"."milestones_milestone"
WHERE
"public"."milestones_milestone"."project_id" = pid
ORDER BY
"estimated_finish" DESC
LIMIT
1
)
ORDER BY
"date_of_entry" DESC
LIMIT
1
) AS "commitment",
-- team planned SPs
(
-- previous milestone id
WITH PreviousMilestone AS (
SELECT
"id"
FROM
"public"."milestones_milestone"
WHERE
"project_id" = pid
ORDER BY
"estimated_finish" DESC OFFSET 1
LIMIT
1
),
-- total SPs
SPs AS (
SELECT
"userstories_userstory"."id" AS userstory_id,
SUM("Projects Points - Points"."value") AS sum
FROM
"public"."userstories_userstory"
LEFT JOIN "public"."userstories_rolepoints" AS "Userstories_Rolepoints" ON "userstories_userstory"."id" = "Userstories_Rolepoints"."user_story_id"
LEFT JOIN "public"."projects_points" AS "Projects Points - Points" ON "Userstories_Rolepoints"."points_id" = "Projects Points - Points"."id"
WHERE
"userstories_userstory"."project_id" = pid
AND "userstories_userstory"."milestone_id" IN (
SELECT
"id"
FROM
"public"."milestones_milestone"
WHERE
"project_id" = pid
ORDER BY
"estimated_finish" DESC
LIMIT
1
)
GROUP BY
"userstories_userstory"."id"
),
SELECT
CASE
WHEN SUM(SPs.sum) IS NULL THEN 0
ELSE SUM(SPs.sum)
END AS "total_SPs"
FROM
SPs,
) AS "storypoints";
RETURN ratio;
END;
$$ LANGUAGE plpgsql;