Skip to content

Commit 5aa4504

Browse files
authored
refactor: flatten declaration_category into typed indicator columns (#3106)
1 parent b3b49de commit 5aa4504

56 files changed

Lines changed: 4483 additions & 2042 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
-- Step 1: Add new indicator columns to declaration
2+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_a_annual_women" numeric;--> statement-breakpoint
3+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_a_annual_men" numeric;--> statement-breakpoint
4+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_a_hourly_women" numeric;--> statement-breakpoint
5+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_a_hourly_men" numeric;--> statement-breakpoint
6+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_b_annual_women" numeric;--> statement-breakpoint
7+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_b_annual_men" numeric;--> statement-breakpoint
8+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_b_hourly_women" numeric;--> statement-breakpoint
9+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_b_hourly_men" numeric;--> statement-breakpoint
10+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_c_annual_women" numeric;--> statement-breakpoint
11+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_c_annual_men" numeric;--> statement-breakpoint
12+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_c_hourly_women" numeric;--> statement-breakpoint
13+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_c_hourly_men" numeric;--> statement-breakpoint
14+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_d_annual_women" numeric;--> statement-breakpoint
15+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_d_annual_men" numeric;--> statement-breakpoint
16+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_d_hourly_women" numeric;--> statement-breakpoint
17+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_d_hourly_men" numeric;--> statement-breakpoint
18+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_e_women" numeric;--> statement-breakpoint
19+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_e_men" numeric;--> statement-breakpoint
20+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_annual_threshold1" numeric;--> statement-breakpoint
21+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_annual_threshold2" numeric;--> statement-breakpoint
22+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_annual_threshold3" numeric;--> statement-breakpoint
23+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_annual_threshold4" numeric;--> statement-breakpoint
24+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_annual_women1" integer;--> statement-breakpoint
25+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_annual_women2" integer;--> statement-breakpoint
26+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_annual_women3" integer;--> statement-breakpoint
27+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_annual_women4" integer;--> statement-breakpoint
28+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_annual_men1" integer;--> statement-breakpoint
29+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_annual_men2" integer;--> statement-breakpoint
30+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_annual_men3" integer;--> statement-breakpoint
31+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_annual_men4" integer;--> statement-breakpoint
32+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_hourly_threshold1" numeric;--> statement-breakpoint
33+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_hourly_threshold2" numeric;--> statement-breakpoint
34+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_hourly_threshold3" numeric;--> statement-breakpoint
35+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_hourly_threshold4" numeric;--> statement-breakpoint
36+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_hourly_women1" integer;--> statement-breakpoint
37+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_hourly_women2" integer;--> statement-breakpoint
38+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_hourly_women3" integer;--> statement-breakpoint
39+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_hourly_women4" integer;--> statement-breakpoint
40+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_hourly_men1" integer;--> statement-breakpoint
41+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_hourly_men2" integer;--> statement-breakpoint
42+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_hourly_men3" integer;--> statement-breakpoint
43+
ALTER TABLE "app_declaration" ADD COLUMN "indicator_f_hourly_men4" integer;--> statement-breakpoint
44+
45+
-- Step 2: Backfill indicator A + C (step 2 categories)
46+
UPDATE "app_declaration" d SET
47+
indicator_a_annual_women = c.women_value,
48+
indicator_a_annual_men = c.men_value
49+
FROM "app_declaration_category" c
50+
WHERE c.siren = d.siren AND c.year = d.year
51+
AND c.step = 2 AND c.category_name = 'Annuelle brute moyenne';--> statement-breakpoint
52+
53+
UPDATE "app_declaration" d SET
54+
indicator_a_hourly_women = c.women_value,
55+
indicator_a_hourly_men = c.men_value
56+
FROM "app_declaration_category" c
57+
WHERE c.siren = d.siren AND c.year = d.year
58+
AND c.step = 2 AND c.category_name = 'Horaire brute moyenne';--> statement-breakpoint
59+
60+
UPDATE "app_declaration" d SET
61+
indicator_c_annual_women = c.women_value,
62+
indicator_c_annual_men = c.men_value
63+
FROM "app_declaration_category" c
64+
WHERE c.siren = d.siren AND c.year = d.year
65+
AND c.step = 2 AND c.category_name = 'Annuelle brute médiane';--> statement-breakpoint
66+
67+
UPDATE "app_declaration" d SET
68+
indicator_c_hourly_women = c.women_value,
69+
indicator_c_hourly_men = c.men_value
70+
FROM "app_declaration_category" c
71+
WHERE c.siren = d.siren AND c.year = d.year
72+
AND c.step = 2 AND c.category_name = 'Horaire brute médiane';--> statement-breakpoint
73+
74+
-- Step 3: Backfill indicator B + D (step 3 categories)
75+
UPDATE "app_declaration" d SET
76+
indicator_b_annual_women = c.women_value,
77+
indicator_b_annual_men = c.men_value
78+
FROM "app_declaration_category" c
79+
WHERE c.siren = d.siren AND c.year = d.year
80+
AND c.step = 3 AND c.category_name = 'Annuelle brute moyenne';--> statement-breakpoint
81+
82+
UPDATE "app_declaration" d SET
83+
indicator_b_hourly_women = c.women_value,
84+
indicator_b_hourly_men = c.men_value
85+
FROM "app_declaration_category" c
86+
WHERE c.siren = d.siren AND c.year = d.year
87+
AND c.step = 3 AND c.category_name = 'Horaire brute moyenne';--> statement-breakpoint
88+
89+
UPDATE "app_declaration" d SET
90+
indicator_d_annual_women = c.women_value,
91+
indicator_d_annual_men = c.men_value
92+
FROM "app_declaration_category" c
93+
WHERE c.siren = d.siren AND c.year = d.year
94+
AND c.step = 3 AND c.category_name = 'Annuelle brute médiane';--> statement-breakpoint
95+
96+
UPDATE "app_declaration" d SET
97+
indicator_d_hourly_women = c.women_value,
98+
indicator_d_hourly_men = c.men_value
99+
FROM "app_declaration_category" c
100+
WHERE c.siren = d.siren AND c.year = d.year
101+
AND c.step = 3 AND c.category_name = 'Horaire brute médiane';--> statement-breakpoint
102+
103+
-- Step 4: Backfill indicator E (step 3 beneficiaries)
104+
UPDATE "app_declaration" d SET
105+
indicator_e_women = c.women_value,
106+
indicator_e_men = c.men_value
107+
FROM "app_declaration_category" c
108+
WHERE c.siren = d.siren AND c.year = d.year
109+
AND c.step = 3 AND c.category_name = 'Bénéficiaires';--> statement-breakpoint
110+
111+
-- Step 5: Backfill indicator F — annual quartiles (step 4)
112+
UPDATE "app_declaration" d SET
113+
indicator_f_annual_threshold1 = c.women_value,
114+
indicator_f_annual_women1 = c.women_count,
115+
indicator_f_annual_men1 = c.men_count
116+
FROM "app_declaration_category" c
117+
WHERE c.siren = d.siren AND c.year = d.year
118+
AND c.step = 4 AND c.category_name = 'annual:1er quartile';--> statement-breakpoint
119+
120+
UPDATE "app_declaration" d SET
121+
indicator_f_annual_threshold2 = c.women_value,
122+
indicator_f_annual_women2 = c.women_count,
123+
indicator_f_annual_men2 = c.men_count
124+
FROM "app_declaration_category" c
125+
WHERE c.siren = d.siren AND c.year = d.year
126+
AND c.step = 4 AND c.category_name = 'annual:2e quartile';--> statement-breakpoint
127+
128+
UPDATE "app_declaration" d SET
129+
indicator_f_annual_threshold3 = c.women_value,
130+
indicator_f_annual_women3 = c.women_count,
131+
indicator_f_annual_men3 = c.men_count
132+
FROM "app_declaration_category" c
133+
WHERE c.siren = d.siren AND c.year = d.year
134+
AND c.step = 4 AND c.category_name = 'annual:3e quartile';--> statement-breakpoint
135+
136+
UPDATE "app_declaration" d SET
137+
indicator_f_annual_threshold4 = c.women_value,
138+
indicator_f_annual_women4 = c.women_count,
139+
indicator_f_annual_men4 = c.men_count
140+
FROM "app_declaration_category" c
141+
WHERE c.siren = d.siren AND c.year = d.year
142+
AND c.step = 4 AND c.category_name = 'annual:4e quartile';--> statement-breakpoint
143+
144+
-- Step 6: Backfill indicator F — hourly quartiles (step 4)
145+
UPDATE "app_declaration" d SET
146+
indicator_f_hourly_threshold1 = c.women_value,
147+
indicator_f_hourly_women1 = c.women_count,
148+
indicator_f_hourly_men1 = c.men_count
149+
FROM "app_declaration_category" c
150+
WHERE c.siren = d.siren AND c.year = d.year
151+
AND c.step = 4 AND c.category_name = 'hourly:1er quartile';--> statement-breakpoint
152+
153+
UPDATE "app_declaration" d SET
154+
indicator_f_hourly_threshold2 = c.women_value,
155+
indicator_f_hourly_women2 = c.women_count,
156+
indicator_f_hourly_men2 = c.men_count
157+
FROM "app_declaration_category" c
158+
WHERE c.siren = d.siren AND c.year = d.year
159+
AND c.step = 4 AND c.category_name = 'hourly:2e quartile';--> statement-breakpoint
160+
161+
UPDATE "app_declaration" d SET
162+
indicator_f_hourly_threshold3 = c.women_value,
163+
indicator_f_hourly_women3 = c.women_count,
164+
indicator_f_hourly_men3 = c.men_count
165+
FROM "app_declaration_category" c
166+
WHERE c.siren = d.siren AND c.year = d.year
167+
AND c.step = 4 AND c.category_name = 'hourly:3e quartile';--> statement-breakpoint
168+
169+
UPDATE "app_declaration" d SET
170+
indicator_f_hourly_threshold4 = c.women_value,
171+
indicator_f_hourly_women4 = c.women_count,
172+
indicator_f_hourly_men4 = c.men_count
173+
FROM "app_declaration_category" c
174+
WHERE c.siren = d.siren AND c.year = d.year
175+
AND c.step = 4 AND c.category_name = 'hourly:4e quartile';--> statement-breakpoint
176+
177+
-- Step 7: Drop the old table
178+
DROP TABLE "app_declaration_category" CASCADE;

0 commit comments

Comments
 (0)