-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoved.sql
More file actions
2982 lines (2805 loc) · 98.5 KB
/
Copy pathremoved.sql
File metadata and controls
2982 lines (2805 loc) · 98.5 KB
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
--moves single claims occuring only once in all tables to separate table
DROP TABLE IF EXISTS singles_p CASCADE;
WITH sub AS
(SELECT *
FROM
(SELECT hapar_id,
year,
string_agg,
LENGTH(string_agg)
FROM
(SELECT hapar_id,
year,
string_agg(is_perm_flag :: VARCHAR, '')
FROM temp_permanent
WHERE hapar_id NOT IN
(SELECT DISTINCT hapar_id
FROM temp_seasonal)
GROUP BY hapar_id,
YEAR) foo) foo2
WHERE length = 1)
SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
bps_eligible_area,
bps_claimed_area,
verified_exclusion,
land_use_area,
land_use,
land_activity,
application_status,
land_leased_out,
lfass_flag,
is_perm_flag,
claim_id_p,
year,
change_note INTO TEMP TABLE singles_p
FROM
(SELECT *
FROM temp_permanent AS t
JOIN sub USING (hapar_id,
year)) foo; -- moves 746,623 rows to singles_p
DELETE FROM temp_permanent AS t USING singles_p
WHERE t.hapar_id = singles_p.hapar_id AND t.year = singles_p.year; -- deletes 746,623 rows from big table
DROP TABLE IF EXISTS singles_s CASCADE;
WITH sub AS
(SELECT *
FROM
(SELECT hapar_id,
year,
string_agg,
LENGTH(string_agg)
FROM
(SELECT hapar_id,
year,
string_agg(is_perm_flag :: VARCHAR, '')
FROM temp_seasonal
WHERE hapar_id NOT IN
(SELECT DISTINCT hapar_id
FROM temp_permanent)
GROUP BY hapar_id,
YEAR) foo) foo2
WHERE length = 1)
SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
bps_eligible_area,
bps_claimed_area,
verified_exclusion,
land_use_area,
land_use,
land_activity,
application_status,
land_leased_out,
lfass_flag,
claim_id_s,
year,
change_note INTO TEMP TABLE singles_s
FROM
(SELECT *
FROM temp_seasonal AS t
JOIN sub USING (hapar_id,
year)) foo; -- moves 46,035 rows to singles_s
DELETE FROM temp_seasonal AS t USING singles_s
WHERE t.hapar_id = singles_s.hapar_id AND t.year = singles_s.year; -- deletes 46,035 rows from big table
--remove duplicate entries with blank or no land_activity and lfass_flag = 'N' - Fixes doubled sum(land_use_area)
WITH lua AS
(SELECT hapar_id,
land_parcel_area,
year,
SUM(land_use_area) AS sum_lua
FROM temp_permanent
GROUP BY hapar_id,
land_parcel_area,
year)
DELETE FROM temp_permanent
WHERE claim_id_p IN (
SELECT claim_id_p
FROM lua
JOIN temp_permanent USING (hapar_id,
land_parcel_area,
year)
WHERE ((sum_lua/land_parcel_area)*100) = 200
AND (land_activity = '' OR land_activity LIKE '%No Activity%')
AND lfass_flag = 'N'); --removes 63 rows
WITH lua AS
(SELECT hapar_id,
land_parcel_area,
year,
SUM(land_use_area) AS sum_lua
FROM temp_seasonal
GROUP BY hapar_id,
land_parcel_area,
year)
DELETE FROM temp_seasonal
WHERE claim_id_s IN (
SELECT claim_id_s
FROM lua
JOIN temp_seasonal USING (hapar_id,
land_parcel_area,
year)
WHERE ((sum_lua/land_parcel_area)*100) = 200
AND (land_activity = '' OR land_activity LIKE '%No Activity%')
AND lfass_flag = 'N'); --removes 1,117 rows
--infers land_use_area = 0 where NULL
UPDATE temp_permanent
SET land_use_area = 0,
change_note = CONCAT (change_note, 'land_use_area inferred 0 where NULL; ')
WHERE land_use_area IS NULL; -- updates 168 rows
UPDATE temp_seasonal
SET land_use_area = 0,
change_note = CONCAT (change_note, 'land_use_area inferred 0 where NULL; ')
WHERE land_use_area IS NULL; -- updates 916 rows
--marks renters in permanent table for removal to seasonal table -- only good for doubled lua
UPDATE temp_permanent AS t
SET claim_id_p = CONCAT('s', t.claim_id_p),
change_note = CONCAT(t.change_note, 'change to seasonal record; ')
FROM temp_permanent
JOIN
(SELECT hapar_id,
year
FROM
(SELECT t.mlc_hahol_id,
t.habus_id,
t.hahol_id,
t.hapar_id,
t.land_activity,
t.YEAR,
string_agg(t.land_leased_out, ''),
ROW_NUMBER () OVER (PARTITION BY hapar_id,
YEAR
ORDER BY hapar_id,
year)
FROM temp_permanent AS t
JOIN
(SELECT *
FROM temp_permanent
WHERE claim_id_p IN
(SELECT claim_id_p
FROM
(SELECT hapar_id,
land_parcel_area,
year,
SUM(land_use_area) AS sum_lua
FROM temp_permanent
GROUP BY hapar_id,
land_parcel_area,
year) lua
JOIN temp_permanent USING (hapar_id,
land_parcel_area,
year)
WHERE ((sum_lua/land_parcel_area)*100) = 200)) foo USING (hapar_id,
year)
GROUP BY t.mlc_hahol_id,
t.habus_id,
t.hahol_id,
t.hapar_id,
t.land_activity,
t.YEAR) t
WHERE ROW_NUMBER > 1) sub USING (hapar_id,
year)
WHERE t.hapar_id = sub.hapar_id
AND t.year = sub.year
AND t.bps_claimed_area = t.land_use_area
AND t.bps_claimed_area <> 0; --updates 40 rows
--marks renters in the seasonal table which have owners in same table -- only good for doubled lua
UPDATE temp_seasonal AS t
SET claim_id_s = CONCAT('s', t.claim_id_s),
change_note = CONCAT(t.change_note, 'change to seasonal; ')
FROM temp_seasonal
JOIN
(SELECT hapar_id,
year
FROM
(SELECT t.mlc_hahol_id,
t.habus_id,
t.hahol_id,
t.hapar_id,
t.land_activity,
t.YEAR,
string_agg(t.land_leased_out, ''),
ROW_NUMBER () OVER (PARTITION BY hapar_id,
YEAR
ORDER BY hapar_id,
year)
FROM temp_seasonal AS t
JOIN
(SELECT *
FROM temp_seasonal
WHERE claim_id_s IN
(SELECT claim_id_s
FROM
(SELECT hapar_id,
land_parcel_area,
year,
SUM(land_use_area) AS sum_lua
FROM temp_seasonal
GROUP BY hapar_id,
land_parcel_area,
year) lua
JOIN temp_seasonal USING (hapar_id,
land_parcel_area,
year)
WHERE ((sum_lua/land_parcel_area)*100) = 200)) foo USING (hapar_id,
year)
GROUP BY t.mlc_hahol_id,
t.habus_id,
t.hahol_id,
t.hapar_id,
t.land_activity,
t.YEAR) t
WHERE ROW_NUMBER > 1) sub USING (hapar_id,
year)
WHERE t.hapar_id = sub.hapar_id
AND t.year = sub.year
AND t.bps_claimed_area = t.land_use_area
AND t.bps_claimed_area <> 0; --updates 1,047 rows
--TODO IMPUTE starts
--impute records to fix missing land_use_area values
WITH lua AS
(SELECT hapar_id,
land_parcel_area,
year,
sum_lua,
percent
FROM
(SELECT hapar_id,
land_parcel_area,
land_use_area,
land_use,
year,
SUM(land_use_area) OVER(PARTITION BY hapar_id, land_parcel_area, year) AS sum_lua,
(SUM(land_use_area) OVER(PARTITION BY hapar_id, land_parcel_area, year) / land_parcel_area) * 100 AS percent
FROM temp_permanent) foo
WHERE sum_lua < land_parcel_area
AND sum_lua > 0
GROUP BY hapar_id,
land_parcel_area,
year,
sum_lua,
percent),
perm AS
(SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
year,
change_note
FROM temp_permanent
GROUP BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
year,
change_note)
INSERT INTO temp_permanent (mlc_hahol_id, habus_id, hahol_id, hapar_id, land_parcel_area, bps_claimed_area, land_use_area, land_use, is_perm_flag, year, change_note)
SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
0 AS bps_claimed_area,
(land_parcel_area - sum_lua) AS diff,
'IMPUTED_RECORD',
'Y',
year,
CONCAT(change_note, 'imputed record to correct difference when sum(land_use_area) < land_parcel_area; ')
FROM lua
JOIN perm USING (hapar_id,
land_parcel_area,
year); -- imputes 34,128 rows
--! seasonal sum_lua should not necessarily = land_parcel_area BUT these could be transferred to imputed LLOs later
WITH lua AS
(SELECT hapar_id,
land_parcel_area,
year,
sum_lua,
percent
FROM
(SELECT hapar_id,
land_parcel_area,
land_use_area,
land_use,
year,
SUM(land_use_area) OVER(PARTITION BY hapar_id, land_parcel_area, year) AS sum_lua,
(SUM(land_use_area) OVER(PARTITION BY hapar_id, land_parcel_area, year) / land_parcel_area) * 100 AS percent
FROM temp_seasonal) foo
WHERE sum_lua < land_parcel_area
AND sum_lua > 0
GROUP BY hapar_id,
land_parcel_area,
year,
sum_lua,
percent),
perm AS
(SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
year,
change_note
FROM temp_seasonal
GROUP BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
year,
change_note)
INSERT INTO temp_seasonal (mlc_hahol_id, habus_id, hahol_id, hapar_id, land_parcel_area, bps_claimed_area, land_use_area, land_use, is_perm_flag, year, change_note)
SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
0 AS bps_claimed_area,
(land_parcel_area - sum_lua) AS diff,
'IMPUTED_RECORD',
'N',
year,
CONCAT(change_note, 'imputed record to correct difference when sum(land_use_area) < land_parcel_area; ')
FROM lua
JOIN perm USING (hapar_id,
land_parcel_area,
year); -- imputes 6,252 records
-- mark imputed records that match existing record with NULL/0 land_use_area
WITH impute AS
(SELECT *
FROM temp_permanent
WHERE land_use = 'IMPUTED_RECORD'), -- 34,079 rows
missing_lua AS
(SELECT *
FROM temp_permanent
WHERE land_use_area IS NULL
OR land_use_area = 0), -- 5,677 rows
doubleLU AS
(SELECT distinct hapar_id
FROM
(SELECT hapar_id,
year,
ROW_NUMBER () OVER (PARTITION BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
year
ORDER BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
year) row_num
FROM missing_lua
JOIN impute USING (mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
year)) foo
WHERE row_num > 1),
elim_doubleLU AS
(SELECT hapar_id,
year,
missing_lua.land_use AS null_lu,
missing_lua.land_use_area AS null_lua,
impute.land_use_area AS fix_lua
FROM missing_lua
INNER JOIN impute USING (mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
year)
WHERE missing_lua.hapar_id NOT IN
(SELECT *
FROM doubleLU))
UPDATE temp_permanent AS p
SET change_note = CONCAT (p.change_note, 'MATCH TO EXISTING RECORD - SET FOR DELETION; ')
FROM (temp_permanent
JOIN elim_doubleLU USING (hapar_id, year)) sub
WHERE p.hapar_id = sub.hapar_id AND p.year = sub.year AND p.land_use = 'IMPUTED_RECORD'; -- updates 842 rows
WITH impute AS
(SELECT *
FROM temp_seasonal
WHERE land_use = 'IMPUTED_RECORD'), -- 6,228 rows
missing_lua AS
(SELECT *
FROM temp_seasonal
WHERE land_use_area IS NULL
OR land_use_area = 0), -- 1,825 rows
doubleLU AS
(SELECT distinct hapar_id
FROM
(SELECT hapar_id,
year,
ROW_NUMBER () OVER (PARTITION BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
year
ORDER BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
year) row_num
FROM missing_lua
JOIN impute USING (mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
year)) foo
WHERE row_num > 1),
elim_doubleLU AS
(SELECT hapar_id,
year,
missing_lua.land_use AS null_lu,
missing_lua.land_use_area AS null_lua,
impute.land_use_area AS fix_lua
FROM missing_lua
INNER JOIN impute USING (mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
year)
WHERE missing_lua.hapar_id NOT IN
(SELECT *
FROM doubleLU))
UPDATE temp_seasonal AS p
SET change_note = CONCAT (p.change_note, 'MATCH TO EXISTING RECORD - SET FOR DELETION; ')
FROM
(SELECT hapar_id,
year
FROM temp_seasonal
JOIN elim_doubleLU USING (hapar_id,
year)) sub
WHERE p.hapar_id = sub.hapar_id
AND p.year = sub.year
AND (land_use_area IS NULL
or land_use_area = 0); -- updates 381 rows
--match marked imputed record and replace NULL/0 land_use_area with calculated area
WITH impute AS
(SELECT *
FROM temp_permanent
WHERE change_note LIKE '%DELETION%'),
missing_lua AS
(SELECT *
FROM temp_permanent
WHERE land_use_area = 0
OR land_use_area IS NULL),
end_tbl AS
(SELECT distinct claim_id_p,
fix_lua
FROM
(SELECT hapar_id,
YEAR,
impute.land_use_area AS fix_lua
FROM impute
JOIN missing_lua USING (hapar_id,
YEAR)) foo
JOIN temp_permanent AS p USING (hapar_id,
year)
WHERE p.hapar_id = foo.hapar_id
AND p.year = foo.year
AND land_use <> 'IMPUTED_RECORD'
AND (land_use_area = 0
OR land_use_area IS NULL))
UPDATE temp_permanent AS p
SET land_use_area = end_tbl.fix_lua,
change_note = CONCAT(p.change_note, 'land_use_area calculated from missing total land_parcel_area for single claims; ')
FROM temp_permanent JOIN end_tbl USING (claim_id_p)
WHERE p.claim_id_p = end_tbl.claim_id_p; -- updates 842 rows
WITH impute AS
(SELECT *
FROM temp_seasonal
WHERE change_note LIKE '%DELETION%'),
missing_lua AS
(SELECT *
FROM temp_seasonal
WHERE land_use_area = 0
OR land_use_area IS NULL),
end_tbl AS
(SELECT distinct claim_id_s,
fix_lua
FROM
(SELECT hapar_id,
YEAR,
impute.land_use_area AS fix_lua
FROM impute
JOIN missing_lua USING (hapar_id,
YEAR)) foo
JOIN temp_seasonal AS p USING (hapar_id,
year)
WHERE p.hapar_id = foo.hapar_id
AND p.year = foo.year
AND land_use <> 'IMPUTED_RECORD'
AND (land_use_area = 0
OR land_use_area IS NULL))
UPDATE temp_seasonal AS p
SET land_use_area = end_tbl.fix_lua,
change_note = CONCAT(p.change_note, 'land_use_area calculated from missing total land_parcel_area for single claims; ')
FROM temp_seasonal JOIN end_tbl USING (claim_id_s)
WHERE p.claim_id_s = end_tbl.claim_id_s; -- updates 381 rows
--delete matched imputed records
DELETE FROM temp_permanent
WHERE change_note LIKE '%DELETION%'; -- removes 842 rows
DELETE FROM temp_seasonal
WHERE change_note LIKE '%DELETION%'; -- removes 381 rows
--*STEP 6. Separate records with no hapar_ids in other table
--move mutually exclusive hapar_ids to separate table
DROP TABLE IF EXISTS p_only;
CREATE TEMP TABLE p_only AS
SELECT *
FROM temp_permanent AS perm
WHERE hapar_id NOT IN
(SELECT DISTINCT hapar_id
FROM temp_seasonal);
DELETE
FROM temp_permanent AS t USING p_only
WHERE t.hapar_id = p_only.hapar_id; --moves 1,110,723 rows
DROP TABLE IF EXISTS s_only;
CREATE TEMP TABLE s_only AS
SELECT *
FROM temp_seasonal AS seas
WHERE hapar_id NOT IN
(SELECT DISTINCT hapar_id
FROM temp_permanent);
DELETE
FROM temp_seasonal AS t USING s_only
WHERE t.hapar_id = s_only.hapar_id; --moves 52,500 rows
--Step 2e. Impute records for LLO Yes in both tables - assume non-saf owners/renters
--! NO need to do this because it will go into end table as owner_mlc.... user_mlc...
--! can just arrange to make note in change_note about non-saf renter
With LLO_yes AS
(SELECT *
FROM temp_permanent_only
WHERE land_leased_out = 'Y')
INSERT INTO temp_seasonal_only (hapar_id, land_parcel_area, bps_eligible_area, bps_claimed_area, verified_exclusion, land_use_area, land_use, land_activity, application_status, land_leased_out, lfass_flag, is_perm_flag, year, change_note)
SELECT hapar_id,
p.land_parcel_area,
p.bps_eligible_area,
p.bps_claimed_area,
p.verified_exclusion,
p.land_use_area,
'IMPUTED_RECORD',
p.land_activity,
p.application_status,
'N',
p.lfass_flag,
'N',
year,
CONCAT(p.change_note, 'imputed record to create non-SAF renter claim where LLO yes in permanent table; ')
FROM LLO_yes
JOIN temp_permanent_only AS p USING (hapar_id,
year,
land_use,
land_use_area)
WHERE p.land_leased_out = 'Y'; -- imputes 5,930 rows
-- 238 LLO yes in seasonal only table (1,542 total)
--! I can't fix these because some of them are separated because its LLO and I suspect a lot of them are not marked for LLO
--TODO check how many duplicate land_use per year ----------- Doug agrees that this does not necessarily need fixing
SELECT DISTINCT hapar_id
FROM
(SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
bps_eligible_area,
bps_claimed_area,
verified_exclusion,
land_use_area,
land_use,
land_activity,
application_status,
land_leased_out,
lfass_flag,
is_perm_flag,
claim_id_p,
YEAR,
ROW_NUMBER () OVER (PARTITION BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
land_use,
year
ORDER BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
land_parcel_area,
land_use,
year) row_num
FROM temp_permanent) foo
WHERE row_num > 1
--TODO -------------------------------------- 833 distinct hapar_id --- 1,189 rows
--finds multiple businesses claiming on same land in permanent table and marks land_use = EXCL as seasonal without changing LLO flag, and vice versa
WITH mult_busses AS
(SELECT *
FROM
(SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
YEAR,
ROW_NUMBER () OVER (PARTITION BY hapar_id,
YEAR)
FROM temp_permanent
GROUP BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
YEAR) foo
WHERE ROW_NUMBER > 1),
bps_claim AS
(SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
year,
SUM(bps_claimed_area) AS sum_bps
FROM temp_permanent
GROUP BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
year)
UPDATE temp_permanent AS t
SET claim_id_p = CONCAT('S', t.claim_id_p),
is_perm_flag = 'N',
change_note = CONCAT(t.change_note, 'S record moved from permanent to seasonal sheet; ')
FROM temp_permanent AS a
JOIN mult_busses USING (mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
year)
JOIN bps_claim USING (mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
year)
WHERE sum_bps <> 0
AND t.mlc_hahol_id = a.mlc_hahol_id
AND t.habus_id = a.habus_id
AND t.hahol_id = a.hahol_id
AND t.hapar_id = a.hapar_id
AND t.year = a.year
AND t.land_use = 'EXCL'; --updates 23 rows
--finds multiple businesses claiming on same land in seasonal table and marks land_use = EXCL as permanent without changing LLO flag, and vice versa
WITH mult_busses AS
(SELECT *
FROM
(SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
YEAR,
ROW_NUMBER () OVER (PARTITION BY hapar_id,
YEAR)
FROM temp_seasonal
GROUP BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
YEAR) foo
WHERE ROW_NUMBER > 1),
bps_claim AS
(SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
year,
SUM(bps_claimed_area) AS sum_bps
FROM temp_seasonal
GROUP BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
year)
UPDATE temp_seasonal AS t
SET claim_id_s = CONCAT('P', t.claim_id_s),
is_perm_flag = 'Y',
change_note = CONCAT(t.change_note, 'P record moved from seasonal to permanent sheet; ')
FROM temp_seasonal AS a
JOIN mult_busses USING (mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
year)
JOIN bps_claim USING (mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
year)
WHERE sum_bps = 0
AND t.mlc_hahol_id = a.mlc_hahol_id
AND t.habus_id = a.habus_id
AND t.hahol_id = a.hahol_id
AND t.hapar_id = a.hapar_id
AND t.year = a.year
AND t.land_use = 'EXCL'; --updates 507 rows
--marks other associated rows of same business to move to permanent based on above
WITH perms AS
(SELECT *
FROM
(SELECT mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
YEAR,
string_agg(claim_id_s :: varchar, '') AS agg
FROM temp_seasonal
GROUP BY mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
YEAR) foo
WHERE agg LIKE '%p%')
UPDATE temp_seasonal AS t
SET is_perm_flag = 'Y',
claim_id_s = 'P' || TRIM('S' from t.claim_id_s),
change_note = CONCAT(t.change_note, 'P record moved from seasonal to permanent sheet; ')
FROM temp_seasonal AS a
JOIN perms USING (mlc_hahol_id,
habus_id,
hahol_id,
hapar_id,
year)
WHERE t.is_perm_flag <> 'Y'
AND t.mlc_hahol_id = a.mlc_hahol_id
AND t.habus_id = a.habus_id
AND t.hahol_id = a.hahol_id
AND t.hapar_id = a.hapar_id
AND t.year = a.year; --updates 694 rows
--Step 3. Join P and S on all possible variables
CREATE TEMP TABLE temp_combine 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,
land_parcel_area,
bps_eligible_area,
bps_claimed_area,
verified_exclusion,
land_use_area,
land_use,
p.land_activity,
p.application_status,
p.land_leased_out AS p_LLO,
s.land_leased_out AS s_LLO,
p.lfass_flag,
s.is_perm_flag,
claim_id_p,
claim_id_s,
year
FROM temp_permanent AS p
JOIN temp_seasonal AS s USING (hahol_id,
hapar_id,
land_parcel_area,
bps_eligible_area,
bps_claimed_area,
verified_exclusion,
land_use_area,
land_use,
year,
land_activity,
application_status,
lfass_flag); -- 2,233 rows (same with/out hahol_id)
--! START JOIN --!
CREATE TEMP TABLE combined (owner_mlc_hahol_id int8,
user_mlc_hahol_id int8,
owner_habus_id int8,
user_habus_id int8,
owner_hahol_id int8,
user_hahol_id int8,
hapar_id int8,
land_parcel_area numeric,
bps_eligible_area numeric,
bps_claimed_area numeric,
verified_exclusion numeric,
land_use_area numeric,
land_use VARCHAR,
land_activity VARCHAR,
application_status VARCHAR,
land_leased_out VARCHAR,
lfass_flag VARCHAR,
is_perm_flag VARCHAR,
claim_id_p int4,
claim_id_s int4,
year int2,
change_note VARCHAR);
INSERT INTO combined (owner_mlc_hahol_id,
user_mlc_hahol_id,
owner_habus_id,
user_habus_id,
owner_hahol_id,
user_hahol_id,
hapar_id,
land_parcel_area,
bps_eligible_area,
bps_claimed_area,
verified_exclusion,
land_use_area,
land_use,
land_activity,
application_status,
land_leased_out,
lfass_flag,
is_perm_flag,
claim_id_p,
claim_id_s,
year,
change_note)
WITH grp_singlehapar_p AS
(SELECT hapar_id,
year,
claim_id_p
FROM
(SELECT hapar_id,
year,
COUNT(*)
FROM temp_permanent
GROUP BY hapar_id,
year
ORDER BY hapar_id,
year) foo
JOIN temp_permanent USING (hapar_id,
year)
WHERE count = 1 ),
grp_singlehapar_s AS
(SELECT hapar_id,
year,
claim_id_s
FROM
(SELECT hapar_id,
year,
COUNT(*)
FROM temp_permanent
GROUP BY hapar_id,
year
ORDER BY hapar_id,
year) foo
JOIN temp_seasonal USING (hapar_id,
year)
WHERE count = 1 )
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,
land_parcel_area,
bps_eligible_area,
p.verified_exclusion, --which one to pick?
land_use_area,
land_use,
p.land_activity,
p.application_status,
'Y' AS land_leased_out, --default to yes because join
p.lfass_flag,
s.is_perm_flag,
claim_id_p,
claim_id_s,
year,
p.change_note
FROM temp_permanent AS p
JOIN temp_seasonal AS s USING (hahol_id,
hapar_id,
land_parcel_area,
bps_eligible_area,
land_use_area,
land_use,
year)
WHERE claim_id_p IN
(SELECT claim_id_p
FROM grp_singlehapar_p)
AND claim_id_s IN
(SELECT claim_id_s
FROM grp_singlehapar_s)
--TODO join by hapar_id
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 p_land_parcel_area,
s.land_parcel_area AS s_land_parcel_area,
p.bps_eligible_area AS p_bps_elig,
s.bps_eligible_area AS s_bps_elig,
p.bps_claimed_area AS p_bps_claim,
s.bps_claimed_area AS s_bps_claim,
p.verified_exclusion,
p.land_use_area AS p_land_use_area,
s.land_use_area AS s_land_use_area,
land_use,
p.land_activity,
p.application_status,
p.land_leased_out AS p_LLO,
s.land_leased_out AS s_LLO,
p.lfass_flag,
s.is_perm_flag,
claim_id_p,
claim_id_s,
year
FROM temp_permanent AS p
JOIN temp_seasonal AS s USING ( hapar_id,
land_use,
year);
--hapar_id not in permanent sheet, LLO yes
CREATE TEMP TABLE switch_to_perm AS
WITH sub AS
(SELECT *
FROM
(SELECT hapar_id,
year,
string_agg(land_leased_out :: VARCHAR, '')
FROM temp_seasonal
GROUP BY hapar_id,
year) foo
WHERE string_agg LIKE '%Y%')
SELECT *
FROM temp_seasonal AS s