-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_data_load_from_raw.sql
1228 lines (1144 loc) · 46.3 KB
/
main_data_load_from_raw.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--*Step 1. Create temp tables, remove select rows, add select columns
-- DROPPED COLUMNS: payment_region, organic_status
-- DROPPED DATA: all year 2019 data, ((land_use = 'EXCL' OR land_use = 'DELETED_LANDUSE') AND (land_use_area = 0 OR land_use_area IS NULL)),
-- duplicate records from payment_region, NULL hapar_id and land_use = ' '
-- recast claim_id column to accept multiple values
-- rename claim_id_p/s so no problems with unique ids between tables
-- create a table for excluded land use codes for use later
--*Step 2. Fix land_parcel_area IS NULL or 0
-- infer land_parcel_area where same hapar_id in different year
-- infer land_parcel_area from land_use_area in same row
-- delete where land_parcel_area IS NULL/0 AND land_use_area = 0
--*Step 3. Fix land_use_area IS NULL or 0
-- copy land_parcel_area for single claims where land_parcel_area = bps_eligible_area
-- copy values from other years where same land_use
-- adjust bps_claimed_area to match land_parcel_area WHERE bps_claimed_area > land_parcel_area
-- adjust land_use_area to match bps_claimed_area
-- delete remaining NULL land_use_area
--*Step 4. Join
-- first join on hapar_id, year, land_use, land_use_area
-- delete from original table where perfect join above
-- second join on hapar_id, year, land_use
-- third join on hapar_id, year, land_use_area
-- fourth join on hapar_id, year
-- deletes problem fourth joins based on specific criteria
-- delete from original table where join above
--*STEP 5. Clean up
-- move leftover mutually exclusive ones to diff tables
--*Step 6. Combine ALL rows into final table
-- mark rows in joined where owner_land_parcel_area <> user_land_parcel_area
-- mark rows in joined where owner_bps_eligible_area <> user_bps_eligible_area
-- mark rows in joined where owner_verified_exclusion <> user_verified_exclusion
-- mark rows in joined where owner_land_activity <> user_land_activity
-- mark rows in joined where owner_application_status <> user_application_status
-- move joined data into last table
-- infer NON-SAF renter where LLO yes
-- infer NON-SAF owner for mutually exclusive users
-- delete zero excluded land use
-- make land_parcel_area match for hapar_ids and year
--*STEP 7: Mark remaining errors // in order of frequency ASC
-- alter table: add error_log to count things that are wrong
-- error: owner_hahol_id <> user_hahol_id
-- error: multiple permanent businesses declaring on same hapar_id
-- error: sum(owner_land_parcel_area) <> sum(user_land_parcel_area)
-- error: sum(owner_bps_eligible_area) <> sum(user_bps_eligible_area)
-- error: doubled user claims in join (one to many)
-- error: seasonal renter with LLO yes
-- error: doubled owner claims in join (many to one)
-- error: multiple seasonal businesses declaring on same hapar_id
-- error: owner_land_use <> user_land_use
-- error: sum(land_use_area) > land_parcel_area
-- error: sum(bps_claimed_area) > land_parcel_area
--*STEP 8: create final table
-- EDIT on 15 February 2021 because DWJ found that user_bps_claimed_area and user_land_use_area were Integer type fields. The fix gained 33,495 rows.
--------------------------------------------------------------------------------------------------------------------------------------------
--*Step 1. Create temp tables, remove select rows, add select columns
-- DROPPED COLUMNS: payment_region, organic_status
-- DROPPED DATA: all year 2019 data, ((land_use = 'EXCL' OR land_use = 'DELETED_LANDUSE') AND (land_use_area = 0 OR land_use_area IS NULL)),
-- duplicate records from payment_region, NULL hapar_id and land_use = ' '
DROP TABLE IF EXISTS temp_permanent CASCADE;
CREATE TEMP TABLE temp_permanent AS
WITH subq AS
(SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
verified_exclusion,
bps_eligible_area,
land_activity,
organic_status,
land_use,
land_use_area,
land_leased_out,
lfass,
bps_claimed_area,
application_status,
payment_region,
is_perm_flag,
year,
claim_id_p,
ROW_NUMBER () OVER (PARTITION BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
verified_exclusion,
bps_eligible_area,
land_activity,
organic_status,
land_use,
land_use_area,
lfass,
bps_claimed_area,
application_status,
is_perm_flag,
year
ORDER BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
verified_exclusion,
bps_eligible_area,
land_activity,
organic_status,
land_use,
land_use_area,
lfass,
bps_claimed_area,
application_status,
is_perm_flag,
year) row_num
FROM rpid.saf_permanent_land_parcels_deliv20190911)
SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
ABS(bps_eligible_area) AS bps_eligible_area, -- fixes 11 rows
bps_claimed_area,
verified_exclusion,
ABS(land_use_area) AS land_use_area, -- fixes 2 rows
land_use,
land_activity,
application_status,
land_leased_out,
lfass AS lfass_flag,
is_perm_flag,
claim_id_p,
YEAR
FROM subq
WHERE row_num < 2 -- removes 55,250 rows
AND hapar_id IS NOT NULL -- removes 609 rows
AND land_use <> '' -- removes 1,761 rows
AND year <> 2019 -- removes 681,711 ROWS
AND application_status NOT LIKE '%Wait%'; --removes 35,966 rows
DELETE
FROM temp_permanent
WHERE (land_use = 'EXCL'
OR land_use = 'DELETED_LANDUSE')
AND (land_use_area = 0
OR land_use_area IS NULL); -- removes 720,741 rows
ALTER TABLE temp_permanent ADD change_note VARCHAR;
---------------------------------------------------------------------1,898,643 in temp_permanent
DROP TABLE IF EXISTS temp_seasonal CASCADE;
CREATE TEMP TABLE temp_seasonal AS
WITH subq AS
(SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
verified_exclusion,
bps_eligible_area,
land_activity,
organic_status,
land_use,
land_use_area,
land_leased_out,
lfass,
bps_claimed_area,
application_status,
payment_region,
is_perm_flag,
year,
claim_id_s,
ROW_NUMBER () OVER (PARTITION BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
verified_exclusion,
bps_eligible_area,
land_activity,
organic_status,
land_use,
land_use_area,
land_leased_out,
lfass,
bps_claimed_area,
application_status,
is_perm_flag,
year
ORDER BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
verified_exclusion,
bps_eligible_area,
land_activity,
organic_status,
land_use,
land_use_area,
land_leased_out,
lfass,
bps_claimed_area,
application_status,
is_perm_flag,
year) row_num
FROM rpid.saf_seasonal_land_parcels_deliv20190911)
SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
ABS(bps_eligible_area) AS bps_eligible_area, -- fixes 1 row
bps_claimed_area,
verified_exclusion,
ABS(land_use_area) AS land_use_area, -- fixes 0 rows
land_use,
land_activity,
application_status,
land_leased_out,
lfass AS lfass_flag,
is_perm_flag,
claim_id_s,
YEAR
FROM subq
WHERE row_num < 2 -- removes 4,362 rows
AND hapar_id IS NOT NULL -- removes 35 rows
AND land_use <> '' -- removes 707 rows
AND year <> 2019 -- removes 30,403 rows
AND application_status NOT LIKE '%Wait%'; -- removes 2,939 rows
DELETE
FROM temp_seasonal
WHERE (land_use = 'EXCL'
OR land_use = 'DELETED_LANDUSE')
AND (land_use_area = 0
OR land_use_area IS NULL); -- removes 76,649 rows
ALTER TABLE temp_seasonal ADD change_note VARCHAR;
---------------------------------------------------------------------173,252 in temp_seasonal
--recast claim_id column to accept multiple values
ALTER TABLE temp_permanent
ALTER COLUMN claim_id_p TYPE VARCHAR;
ALTER TABLE temp_seasonal
ALTER COLUMN claim_id_s TYPE VARCHAR;
--rename claim_id_p/s so no problems with unique ids between tables
UPDATE temp_permanent
SET claim_id_p = 'P' || claim_id_p
WHERE claim_id_p NOT LIKE '%P%';
UPDATE temp_seasonal
SET claim_id_s = 'S' || claim_id_s
WHERE claim_id_s NOT LIKE '%S%';
-- create a table for excluded land use codes for use later
DROP TABLE IF EXISTS excl;
CREATE TEMP TABLE excl (land_use VARCHAR(30),
descript VARCHAR(30));
INSERT INTO excl (land_use, descript)
VALUES ('BLU-GLS', 'Blueberries - glasshouse'),
('BRA', 'Bracken'),
('BUI', 'Building'),
('EXCL','Generic exclusion'),
('FSE', 'Foreshore'),
('GOR', 'Gorse'),
('LLO', 'Land let out'),
('MAR', 'Marsh'),
('RASP-GLS', 'Raspberries - glasshouse'),
('ROAD', 'Road'),
('ROK', 'Rocks'),
('SCB', 'Scrub'),
('SCE', 'Scree'),
('STRB-GLS', 'Strawberries - glasshouse'),
('TOM-GLS', 'Tomatoes - glasshouse'),
('TREE', 'Trees'),
('TREES', 'Trees'),
('TURF', 'Trees'),
('WAT', 'Water');
--*Step 2. Fix land_parcel_area IS NULL or 0
--infer land_parcel_area where same hapar_id in different year
UPDATE temp_permanent AS t
SET land_parcel_area = sub.land_parcel_area,
change_note = CONCAT(t.change_note, 'land_parcel_area inferred from same hapar_id; ')
FROM
(SELECT *
FROM temp_permanent
WHERE land_parcel_area IS NOT NULL
OR land_parcel_area <> 0) sub
WHERE t.hapar_id = sub.hapar_id
AND t.land_parcel_area IS NULL; -- updates 3 rows
UPDATE temp_seasonal AS t
SET land_parcel_area = sub.land_parcel_area,
change_note = CONCAT(t.change_note, 'land_parcel_area inferred from same hapar_id; ')
FROM
(SELECT *
FROM temp_seasonal
WHERE land_parcel_area IS NOT NULL
OR land_parcel_area <> 0) sub
WHERE t.hapar_id = sub.hapar_id
AND t.land_parcel_area IS NULL; -- updates 5 rows
--infer land_parcel_area from land_use_area in same row
UPDATE temp_permanent
SET land_parcel_area = land_use_area,
change_note = CONCAT(change_note, 'land_parcel_area inferred from land_use_area in single claim row; ')
WHERE land_parcel_area IS NULL; -- updates 13 rows
UPDATE temp_seasonal
SET land_parcel_area = land_use_area,
change_note = CONCAT(change_note, 'land_parcel_area inferred from land_use_area in single claim row; ')
WHERE land_parcel_area IS NULL; -- updates 1 rows
--delete where land_parcel_area IS NULL/0 AND land_use_area = 0
DELETE
FROM temp_permanent
WHERE (land_parcel_area IS NULL
AND land_use_area = 0)
OR (land_parcel_area = 0
AND land_use_area =0); -- deletes 19 rows
DELETE
FROM temp_seasonal
WHERE (land_parcel_area IS NULL
AND land_use_area = 0)
OR (land_parcel_area = 0
AND land_use_area =0); -- deletes 1 rows
--*Step 3. Fix land_use_area IS NULL or 0
--copy land_parcel_area for single claims where land_parcel_area = bps_eligible_area
WITH sub AS
(SELECT hapar_id,
year
FROM
(SELECT hapar_id,
year,
COUNT(land_use) as lu_count,
SUM(land_use_area) as sum_lu
FROM temp_permanent AS tp
GROUP BY hapar_id,
year) foo
WHERE lu_count = 1
AND (sum_lu = 0 OR sum_lu IS NULL))
UPDATE temp_permanent
SET land_use_area = p.land_parcel_area,
change_note = CONCAT(p.change_note, 'land_use_area inferred where land_parcel_area = bps_eligible_area for single claims; ')
FROM sub
JOIN temp_permanent AS p USING (hapar_id,
year)
WHERE temp_permanent.hapar_id = sub.hapar_id
AND temp_permanent.year = sub.year
AND temp_permanent.land_parcel_area = temp_permanent.bps_eligible_area; -- updates 688 rows
WITH sub AS
(SELECT hapar_id,
year
FROM
(SELECT hapar_id,
year,
COUNT(land_use) as lu_count,
SUM(land_use_area) as sum_lu
FROM temp_seasonal AS ts
GROUP BY hapar_id,
year) foo
WHERE lu_count = 1
AND (sum_lu = 0 OR sum_lu IS NULL))
UPDATE temp_seasonal
SET land_use_area = s.land_parcel_area,
change_note = CONCAT(s.change_note, 'land_use_area inferred where land_parcel_area = bps_eligible_area for single claims; ')
FROM sub
JOIN temp_seasonal AS s USING (hapar_id,
year)
WHERE temp_seasonal.hapar_id = sub.hapar_id
AND temp_seasonal.year = sub.year
AND temp_seasonal.land_parcel_area = temp_seasonal.bps_eligible_area; -- updates 842 rows
-- copy values from other years where same land_use
WITH sub1 AS
(SELECT *
FROM
(SELECT hapar_id,
sum(land_use_area) OVER(PARTITION BY hapar_id, year) AS sum_lua,
land_use,
year
FROM temp_permanent) foo
WHERE sum_lua IS NULL),
sub2 AS
(SELECT hapar_id,
land_use,
p.land_use_area AS fix_lu
FROM sub1
JOIN temp_permanent AS p USING (hapar_id,
land_use)
WHERE p.land_use_area IS NOT NULL
GROUP BY hapar_id,
land_use,
p.land_use_area
ORDER BY hapar_id)
UPDATE temp_permanent AS p
SET land_use_area = fix_lu,
change_note = CONCAT(p.change_note, 'land_use_area inferred from other year where same land_use; ')
FROM temp_permanent
JOIN sub2 USING (hapar_id,
land_use)
WHERE p.hapar_id = sub2.hapar_id
AND p.land_use = sub2.land_use
AND (p.land_use_area IS NULL
OR p.land_use_area = 0); -- updates 73 rows
WITH sub1 AS
(SELECT *
FROM
(SELECT hapar_id,
sum(land_use_area) OVER(PARTITION BY hapar_id, year) AS sum_lua,
land_use,
year
FROM temp_seasonal) foo
WHERE sum_lua IS NULL),
sub2 AS
(SELECT hapar_id,
land_use,
s.land_use_area AS fix_lu
FROM sub1
JOIN temp_seasonal AS s USING (hapar_id,
land_use)
WHERE s.land_use_area IS NOT NULL
GROUP BY hapar_id,
land_use,
s.land_use_area
ORDER BY hapar_id)
UPDATE temp_seasonal AS s
SET land_use_area = fix_lu,
change_note = CONCAT(s.change_note, 'land_use_area inferred from other year where same land_use; ')
FROM temp_seasonal
JOIN sub2 USING (hapar_id,
land_use)
WHERE s.hapar_id = sub2.hapar_id
AND s.land_use = sub2.land_use
AND (s.land_use_area IS NULL
OR s.land_use_area = 0); -- updates 113 rows
-- adjust bps_claimed_area to match land_parcel_area WHERE bps_claimed_area > land_parcel_area
UPDATE temp_permanent
SET bps_claimed_area = land_parcel_area,
change_note = CONCAT(change_note, 'adjust bps_claimed_area to match land_parcel_area where bps > parcel; ')
WHERE bps_claimed_area > land_parcel_area; -- updates 660 rows
UPDATE temp_seasonal
SET bps_claimed_area = land_parcel_area,
change_note = CONCAT(change_note, 'adjust bps_claimed_area to match land_parcel_area where bps > parcel; ')
WHERE bps_claimed_area > land_parcel_area; -- updates 49 rows
--adjust land_use_area to match bps_claimed_area
UPDATE temp_permanent
SET land_use_area = bps_claimed_area,
change_note = CONCAT(change_note, 'adjust owner land_use_area to match bps_claimed_area; ')
WHERE bps_claimed_area <> land_use_area
AND bps_claimed_area <> 0
AND bps_claimed_area <= land_parcel_area; -- updates 186,183 rows
UPDATE temp_seasonal
SET land_use_area = bps_claimed_area,
change_note = CONCAT(change_note, 'adjust user land_use_area to match bps_claimed_area; ')
WHERE bps_claimed_area <> land_use_area
AND bps_claimed_area <> 0
AND bps_claimed_area <= land_parcel_area;; -- updates 21,385 rows
--delete remaining NULL land_use_area
DELETE FROM
temp_permanent
WHERE land_use_area IS NULL; --deletes 349 rows
DELETE FROM
temp_seasonal
WHERE land_use_area IS NULL; --deletes 1,168 rows
--*Step 4. Join
--first join on hapar_id, year, land_use, land_use_area
DROP TABLE IF EXISTS joined;
SELECT * INTO TEMP TABLE joined
FROM
(SELECT p.mlc_hahol_id AS owner_mlc_hahol_id,
s.mlc_hahol_id AS user_mlc_hahol_id,
p.habus_id AS owner_habus_id,
s.habus_id AS user_habus_id,
p.hahol_id AS owner_hahol_id,
s.hahol_id AS user_hahol_id,
hapar_id,
p.land_parcel_area AS owner_land_parcel_area,
s.land_parcel_area AS user_land_parcel_area,
p.bps_eligible_area AS owner_bps_eligible_area,
s.bps_eligible_area AS user_bps_eligible_area,
p.bps_claimed_area AS owner_bps_claimed_area,
s.bps_claimed_area AS user_bps_claimed_area,
p.verified_exclusion AS owner_verified_exclusion,
s.verified_exclusion AS user_verified_exclusion,
p.land_use_area AS owner_land_use_area,
s.land_use_area AS user_land_use_area,
p.land_use AS owner_land_use,
s.land_use AS user_land_use,
p.land_activity AS owner_land_activity,
s.land_activity AS user_land_activity,
p.application_status AS owner_application_status,
s.application_status AS user_application_status,
p.land_leased_out AS owner_land_leased_out,
s.land_leased_out AS user_land_leased_out,
p.lfass_flag AS owner_lfass_flag,
s.lfass_flag AS user_lfass_flag,
CONCAT(claim_id_p, ', ', claim_id_s) AS claim_id,
year,
CASE
WHEN p.change_note IS NOT NULL
AND s.change_note IS NOT NULL THEN CONCAT('first join; ', p.change_note, s.change_note)
WHEN p.change_note IS NULL
AND s.change_note IS NOT NULL THEN CONCAT('first join; ', s.change_note)
WHEN s.change_note IS NULL
AND p.change_note IS NOT NULL THEN CONCAT('first join; ', p.change_note)
WHEN p.change_note IS NULL
AND s.change_note IS NULL THEN 'first join; '
END AS change_note
FROM temp_permanent AS p
JOIN temp_seasonal AS s USING (hapar_id,
year,
land_use,
land_use_area)) foo
WHERE owner_land_leased_out = 'Y'
OR (owner_land_leased_out = 'N'
AND owner_bps_claimed_area = 0); --33,481 rows
--delete from original table where perfect join above
WITH joined_ids AS (
SELECT SPLIT_PART(claim_id, ', ', 1) AS claim_id_p,
SPLIT_PART(claim_id, ', ', 2) AS claim_id_s
FROM joined)
DELETE
FROM temp_permanent AS t USING joined_ids AS a
WHERE t.claim_id_p = a.claim_id_p; -- 33,108 rows
WITH joined_ids AS (
SELECT SPLIT_PART(claim_id, ', ', 1) AS claim_id_p,
SPLIT_PART(claim_id, ', ', 2) AS claim_id_s
FROM joined)
DELETE
FROM temp_seasonal AS t USING joined_ids AS a
WHERE t.claim_id_s = a.claim_id_s; --33,481 rows
--second join on hapar_id, year, land_use
WITH all_joined AS (
SELECT p.mlc_hahol_id AS owner_mlc_hahol_id,
s.mlc_hahol_id AS user_mlc_hahol_id,
p.habus_id AS owner_habus_id,
s.habus_id AS user_habus_id,
p.hahol_id AS owner_hahol_id,
s.hahol_id AS user_hahol_id,
hapar_id,
p.land_parcel_area AS owner_land_parcel_area,
s.land_parcel_area AS user_land_parcel_area,
p.bps_eligible_area AS owner_bps_eligible_area,
s.bps_eligible_area AS user_bps_eligible_area,
p.bps_claimed_area AS owner_bps_claimed_area,
s.bps_claimed_area AS user_bps_claimed_area,
p.verified_exclusion AS owner_verified_exclusion,
s.verified_exclusion AS user_verified_exclusion,
p.land_use_area AS owner_land_use_area,
s.land_use_area AS user_land_use_area,
p.land_use AS owner_land_use,
s.land_use AS user_land_use,
p.land_activity AS owner_land_activity,
s.land_activity AS user_land_activity,
p.application_status AS owner_application_status,
s.application_status AS user_application_status,
p.land_leased_out AS owner_land_leased_out,
s.land_leased_out AS user_land_leased_out,
p.lfass_flag AS owner_lfass_flag,
s.lfass_flag AS user_lfass_flag,
CONCAT(claim_id_p, ', ', claim_id_s) AS claim_id,
year,
CASE
WHEN p.change_note IS NOT NULL
AND s.change_note IS NOT NULL THEN CONCAT('second join; ', p.change_note, s.change_note)
WHEN p.change_note IS NULL
AND s.change_note IS NOT NULL THEN CONCAT('second join; ', s.change_note)
WHEN s.change_note IS NULL
AND p.change_note IS NOT NULL THEN CONCAT('second join; ', p.change_note)
WHEN p.change_note IS NULL
AND s.change_note IS NULL THEN 'second join; '
END AS change_note
FROM temp_permanent AS p
JOIN temp_seasonal AS s USING (hapar_id,
year,
land_use))
INSERT INTO joined
SELECT *
FROM all_joined
WHERE claim_id NOT IN (SELECT claim_id FROM joined); -- 16,447 rows
--third join on hapar_id, year, land_use_area
WITH all_joined AS (
SELECT p.mlc_hahol_id AS owner_mlc_hahol_id,
s.mlc_hahol_id AS user_mlc_hahol_id,
p.habus_id AS owner_habus_id,
s.habus_id AS user_habus_id,
p.hahol_id AS owner_hahol_id,
s.hahol_id AS user_hahol_id,
hapar_id,
p.land_parcel_area AS owner_land_parcel_area,
s.land_parcel_area AS user_land_parcel_area,
p.bps_eligible_area AS owner_bps_eligible_area,
s.bps_eligible_area AS user_bps_eligible_area,
p.bps_claimed_area AS owner_bps_claimed_area,
s.bps_claimed_area AS user_bps_claimed_area,
p.verified_exclusion AS owner_verified_exclusion,
s.verified_exclusion AS user_verified_exclusion,
p.land_use_area AS owner_land_use_area,
s.land_use_area AS user_land_use_area,
p.land_use AS owner_land_use,
s.land_use AS user_land_use,
p.land_activity AS owner_land_activity,
s.land_activity AS user_land_activity,
p.application_status AS owner_application_status,
s.application_status AS user_application_status,
p.land_leased_out AS owner_land_leased_out,
s.land_leased_out AS user_land_leased_out,
p.lfass_flag AS owner_lfass_flag,
s.lfass_flag AS user_lfass_flag,
CONCAT(claim_id_p, ', ', claim_id_s) AS claim_id,
year,
CASE
WHEN p.change_note IS NOT NULL
AND s.change_note IS NOT NULL THEN CONCAT('third join; ', p.change_note, s.change_note)
WHEN p.change_note IS NULL
AND s.change_note IS NOT NULL THEN CONCAT('third join; ', s.change_note)
WHEN s.change_note IS NULL
AND p.change_note IS NOT NULL THEN CONCAT('third join; ', p.change_note)
WHEN p.change_note IS NULL
AND s.change_note IS NULL THEN 'third join; '
END AS change_note
FROM temp_permanent AS p
JOIN temp_seasonal AS s USING (hapar_id,
year,
land_use_area))
INSERT INTO joined
SELECT *
FROM all_joined
WHERE claim_id NOT IN (SELECT claim_id FROM joined); --3,373 rows
--fourth join on hapar_id, year
WITH all_joined AS
(SELECT p.mlc_hahol_id AS owner_mlc_hahol_id,
s.mlc_hahol_id AS user_mlc_hahol_id,
p.habus_id AS owner_habus_id,
s.habus_id AS user_habus_id,
p.hahol_id AS owner_hahol_id,
s.hahol_id AS user_hahol_id,
hapar_id,
p.land_parcel_area AS owner_land_parcel_area,
s.land_parcel_area AS user_land_parcel_area,
p.bps_eligible_area AS owner_bps_eligible_area,
s.bps_eligible_area AS user_bps_eligible_area,
p.bps_claimed_area AS owner_bps_claimed_area,
s.bps_claimed_area AS user_bps_claimed_area,
p.verified_exclusion AS owner_verified_exclusion,
s.verified_exclusion AS user_verified_exclusion,
p.land_use_area AS owner_land_use_area,
s.land_use_area AS user_land_use_area,
p.land_use AS owner_land_use,
s.land_use AS user_land_use,
p.land_activity AS owner_land_activity,
s.land_activity AS user_land_activity,
p.application_status AS owner_application_status,
s.application_status AS user_application_status,
p.land_leased_out AS owner_land_leased_out,
s.land_leased_out AS user_land_leased_out,
p.lfass_flag AS owner_lfass_flag,
s.lfass_flag AS user_lfass_flag,
CONCAT(claim_id_p, ', ', claim_id_s) AS claim_id,
year,
CASE
WHEN p.change_note IS NOT NULL
AND s.change_note IS NOT NULL THEN CONCAT('fourth join; ', p.change_note, s.change_note)
WHEN p.change_note IS NULL
AND s.change_note IS NOT NULL THEN CONCAT('fourth join; ', s.change_note)
WHEN s.change_note IS NULL
AND p.change_note IS NOT NULL THEN CONCAT('fourth join; ', p.change_note)
WHEN p.change_note IS NULL
AND s.change_note IS NULL THEN 'fourth join; '
END AS change_note
FROM temp_permanent AS p
JOIN temp_seasonal AS s USING (hapar_id,
year))
INSERT INTO joined
SELECT *
FROM all_joined
WHERE claim_id NOT IN
(SELECT claim_id
FROM joined); -- 9,773 rows
-- deletes problem fourth joins based on specific criteria
DELETE
FROM joined
WHERE change_note LIKE '%fourth join%'
AND ((owner_land_use IN
(SELECT land_use
FROM excl)
AND user_bps_claimed_area <> 0) -- 1,364 rows
OR (user_land_use IN
(SELECT land_use
FROM excl)
AND owner_bps_claimed_area <> 0) -- 483 rows
OR (owner_land_use NOT IN
(SELECT land_use
FROM excl)
AND user_land_use IN
(SELECT land_use
FROM excl))); --2,152 rows // total 3,517
--delete from original table where join above
WITH joined_ids AS (
SELECT SPLIT_PART(claim_id, ', ', 1) AS claim_id_p
FROM joined)
DELETE
FROM temp_permanent AS t USING joined_ids AS a
WHERE t.claim_id_p = a.claim_id_p; -- 22,463 rows
WITH joined_ids AS (
SELECT SPLIT_PART(claim_id, ', ', 2) AS claim_id_s
FROM joined)
DELETE
FROM temp_seasonal AS t USING joined_ids AS a
WHERE t.claim_id_s = a.claim_id_s; -- 23,493 rows
--*STEP 5. Clean up
--move leftover mutually exclusive ones to diff tables
DROP TABLE IF EXISTS combine;
SELECT mlc_hahol_id AS owner_mlc_hahol_id,
NULL :: BIGINT AS user_mlc_hahol_id,
habus_id AS owner_habus_id,
NULL :: BIGINT AS user_habus_id,
hahol_id AS owner_hahol_id,
NULL :: BIGINT AS user_hahol_id,
hapar_id,
land_parcel_area AS owner_land_parcel_area,
NULL :: NUMERIC AS user_land_parcel_area,
bps_eligible_area AS owner_bps_eligible_area,
NULL :: NUMERIC AS user_bps_eligible_area,
bps_claimed_area AS owner_bps_claimed_area,
NULL :: NUMERIC AS user_bps_claimed_area,
verified_exclusion AS owner_verified_exclusion,
NULL :: NUMERIC user_verified_exclusion,
land_use_area AS owner_land_use_area,
NULL :: NUMERIC AS user_land_use_area,
land_use AS owner_land_use,
NULL :: VARCHAR AS user_land_use,
land_activity AS owner_land_activity,
NULL :: VARCHAR AS user_land_activity,
application_status AS owner_application_status,
NULL :: VARCHAR AS user_application_status,
land_leased_out AS owner_land_leased_out,
NULL :: VARCHAR AS user_land_leased_out,
lfass_flag AS owner_lfass_flag,
NULL :: VARCHAR AS user_lfass_flag,
claim_id_p AS claim_id,
year,
change_note INTO TEMP TABLE combine
FROM temp_permanent; --1,842,704 rows
INSERT INTO combine
SELECT NULL :: BIGINT AS owner_mlc_hahol_id,
mlc_hahol_id AS user_mlc_hahol_id,
NULL :: BIGINT AS owner_habus_id,
habus_id AS user_habus_id,
NULL :: BIGINT AS owner_hahol_id,
hahol_id AS user_hahol_id,
hapar_id,
NULL :: NUMERIC AS owner_land_parcel_area,
land_parcel_area AS user_land_parcel_area,
NULL :: NUMERIC AS owner_bps_eligible_area,
bps_eligible_area AS user_bps_eligible_area,
NULL :: NUMERIC AS owner_bps_claimed_area,
bps_claimed_area AS user_bps_claimed_area,
NULL :: NUMERIC AS owner_verified_exclusion,
verified_exclusion AS user_verified_exclusion,
NULL :: NUMERIC AS owner_land_use_area,
land_use_area AS user_land_use_area,
NULL :: VARCHAR AS owner_land_use,
land_use AS user_land_use,
NULL :: VARCHAR AS owner_land_activity,
land_activity AS user_land_activity,
NULL :: VARCHAR AS owner_application_status,
application_status AS user_application_status,
NULL :: VARCHAR AS owner_land_leased_out,
land_leased_out AS user_land_leased_out,
NULL :: VARCHAR AS owner_lfass_flag,
lfass_flag AS user_lfass_flag,
claim_id_s AS claim_id,
year,
change_note
FROM temp_seasonal; --last 115,109 rows
DROP TABLE temp_permanent;
DROP TABLE temp_seasonal;
--*Step 6. Combine ALL rows into final table
DROP TABLE IF EXISTS final;
CREATE TEMP TABLE final AS
SELECT owner_mlc_hahol_id,
user_mlc_hahol_id,
owner_habus_id,
user_habus_id,
owner_hahol_id,
user_hahol_id,
hapar_id,
CASE
WHEN owner_land_parcel_area IS NULL THEN user_land_parcel_area
WHEN user_land_parcel_area IS NULL THEN owner_land_parcel_area
END AS land_parcel_area,
CASE
WHEN owner_bps_eligible_area IS NULL THEN user_bps_eligible_area
WHEN user_bps_eligible_area IS NULL THEN owner_bps_eligible_area
END AS bps_eligible_area,
owner_bps_claimed_area,
user_bps_claimed_area,
CASE
WHEN owner_verified_exclusion IS NULL THEN user_verified_exclusion
WHEN user_verified_exclusion IS NULL THEN owner_verified_exclusion
END AS verified_exclusion,
owner_land_use_area,
user_land_use_area,
owner_land_use,
user_land_use,
CASE
WHEN owner_land_activity IS NULL THEN user_land_activity
WHEN user_land_activity IS NULL THEN owner_land_activity
END AS land_activity,
CASE
WHEN owner_application_status IS NULL THEN user_application_status
WHEN user_application_status IS NULL THEN owner_application_status
END AS application_status,
owner_land_leased_out,
user_land_leased_out,
owner_lfass_flag,
user_lfass_flag,
claim_id,
year,
change_note
FROM combine; -- moves 1,957,813 rows
--mark rows in joined where owner_land_parcel_area <> user_land_parcel_area
UPDATE joined
SET change_note = CONCAT(change_note, 'assume land_parcel_area = owner_land_parcel_area when owner > user; ')
WHERE owner_land_parcel_area > user_land_parcel_area; -- updates 167 rows
UPDATE joined
SET change_note = CONCAT(change_note, 'assume land_parcel_area = user_land_parcel_area when user > owner; ')
WHERE owner_land_parcel_area < user_land_parcel_area; -- updates 179 rows
--mark rows in joined where owner_bps_eligible_area <> user_bps_eligible_area
UPDATE joined
SET change_note = CONCAT(change_note, 'assume bps_eligible_area = owner_bps_eligible_area when owner > user; ')
WHERE owner_bps_eligible_area > user_bps_eligible_area; -- updates 173 rows
UPDATE joined
SET change_note = CONCAT(change_note, 'assume bps_eligible_area = user_bps_eligible_area when user > owner; ')
WHERE owner_bps_eligible_area < user_bps_eligible_area; -- updates 241 rows
--mark rows in joined where owner_verified_exclusion <> user_verified_exclusion
UPDATE joined
SET change_note = CONCAT(change_note, 'assume verified_exclusion = owner_verified_exclusion when owner > user; ')
WHERE owner_verified_exclusion > user_verified_exclusion; -- updates 2,208 rows
UPDATE joined
SET change_note = CONCAT(change_note, 'assume verified_exclusion = user_verified_exclusion WHEN user > owner; ')
WHERE owner_verified_exclusion < user_verified_exclusion; -- updates 2,461 rows
--mark rows in joined where owner_land_activity <> user_land_activity
UPDATE joined
SET change_note = CONCAT(change_note, 'owner and user land_activity choice based on assumption user knows best; ')
WHERE owner_land_activity <> user_land_activity; -- updates 40,793 rows
--mark rows in joined where owner_application_status <> user_application_status
UPDATE joined
SET change_note = CONCAT(change_note, 'application status assumed under action/assessment if either owner or user says so; ')
WHERE (owner_application_status LIKE '%Action%'
OR user_application_status LIKE '%Action')
AND owner_application_status <> user_application_status; -- updates 1,257 rows
-- move joined data into last table
INSERT INTO final
SELECT owner_mlc_hahol_id,
user_mlc_hahol_id,
owner_habus_id,
user_habus_id,
owner_hahol_id,
user_hahol_id,
hapar_id,
CASE
WHEN owner_land_parcel_area > user_land_parcel_area THEN owner_land_parcel_area
WHEN user_land_parcel_area > owner_land_parcel_area THEN user_land_parcel_area
ELSE owner_land_parcel_area
END AS land_parcel_area, --changes 346 rows with largest change = 5.77 ha
-- total change = 79.63
CASE
WHEN owner_bps_eligible_area > user_bps_eligible_area THEN owner_bps_eligible_area
WHEN user_bps_eligible_area > owner_bps_eligible_area THEN user_bps_eligible_area
ELSE owner_bps_eligible_area
END AS bps_eligible_area, --changes 414 rows with largest change = 273.3 ha
-- total change = 797.27
owner_bps_claimed_area,
user_bps_claimed_area,
CASE
WHEN owner_verified_exclusion > user_verified_exclusion THEN owner_verified_exclusion
WHEN user_verified_exclusion > owner_verified_exclusion THEN user_verified_exclusion
ELSE owner_verified_exclusion
END AS verified_exclusion, --changes 4,670 rows with largest change = 4,208.76 ha
-- total change = 50,680.66
owner_land_use_area,
user_land_use_area,
owner_land_use,
user_land_use,
CASE
WHEN owner_land_activity = '' THEN user_land_activity
WHEN user_land_activity = '' THEN owner_land_activity
WHEN (user_land_activity = 'No Activity' OR user_land_activity = 'Unspecified') AND owner_land_activity <> '' THEN owner_land_activity
ELSE user_land_activity
END AS land_activity, --changes 40,793 rows
CASE
WHEN owner_application_status = user_application_status THEN owner_application_status
WHEN owner_application_status LIKE '%Action%' AND owner_application_status <> user_application_status THEN owner_application_status
WHEN user_application_status LIKE '%Action%' AND owner_application_status <> user_application_status THEN user_application_status
ELSE owner_application_status
END AS application_status, --changes 1,789 rows
owner_land_leased_out,
user_land_leased_out,
owner_lfass_flag,
user_lfass_flag,
claim_id,
year,
change_note
FROM joined; -- moves 59,557 rows
-- infer NON-SAF renter where LLO yes
UPDATE final
SET user_land_use = 'NON_SAF',
change_note = CONCAT(change_note, 'infer non-SAF renter; ')
WHERE user_habus_id IS NULL
AND owner_land_leased_out = 'Y'
and owner_land_use NOT IN
(SELECT land_use
FROM excl); --updates 6,683 rows
-- infer NON-SAF owner for mutually exclusive users
UPDATE final
SET owner_land_use = 'NON_SAF',
change_note = CONCAT(change_note, 'infer non-SAF owner; ')
WHERE owner_habus_id IS NULL
AND user_land_use NOT IN
(SELECT land_use
FROM excl); --updates 86,648 rows
-- delete zero excluded land use
DELETE
FROM FINAL
WHERE user_land_use_area = 0
AND user_land_use IN
(SELECT land_use
FROM excl); -- 42 rows
DELETE
FROM final
WHERE owner_land_use_area = 0
AND owner_land_use IN
(SELECT land_use
FROM excl); -- 262 rows
-- make land_parcel_area match for hapar_ids and year (biggest diff is 2.35 ha but 98.6% are less than 0.5)
UPDATE final AS f
SET land_parcel_area = max_parcel,
change_note = CONCAT(f.change_note, 'adjust land_parcel_area to max(parcel) where different in same year; ')
FROM
(SELECT hapar_id,
YEAR,
max(land_parcel_area) AS max_parcel,
min(land_parcel_area) AS min_parcel
FROM FINAL
GROUP BY hapar_id,
YEAR) foo