-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path012_check_for_duplicates_in_shipments.sql
More file actions
48 lines (44 loc) · 1.74 KB
/
012_check_for_duplicates_in_shipments.sql
File metadata and controls
48 lines (44 loc) · 1.74 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
DROP FUNCTION IF EXISTS check_for_duplicates_in_shipments(INTEGER);
CREATE OR REPLACE FUNCTION check_for_duplicates_in_shipments(
annual_report_upload_id INTEGER
) RETURNS INTEGER[]
LANGUAGE plpgsql
AS $$
DECLARE
table_name TEXT;
duplicates_ids INTEGER[];
BEGIN
table_name = 'trade_sandbox_' || annual_report_upload_id;
EXECUTE '
WITH duplicates AS (
SELECT DISTINCT sb.id
FROM ' || table_name || ' AS sb
JOIN geo_entities AS ge ON ge.iso_code2 = sb.trading_partner
JOIN trade_shipments AS s ON sb.reported_taxon_concept_id = s.reported_taxon_concept_id
AND sb.appendix = s.appendix AND sb.year::integer = s.year
JOIN trade_annual_report_uploads AS aru ON aru.id = ' || annual_report_upload_id || '
WHERE (
(aru.point_of_view = ''I'' AND NOT s.reported_by_exporter AND aru.trading_country_id = s.importer_id) AND
(COALESCE(sb.import_permit,'''') = COALESCE(s.import_permit_number,''''))
)
OR
(
(aru.point_of_view = ''I'' AND s.reported_by_exporter AND ge.id = s.importer_id) AND
(COALESCE(sb.import_permit,'''') = COALESCE(s.import_permit_number,''''))
)
OR
(
(aru.point_of_view = ''E'' AND s.reported_by_exporter AND aru.trading_country_id = s.exporter_id) AND
(COALESCE(sb.export_permit,'''') = COALESCE(s.export_permit_number,''''))
)
OR
(
(aru.point_of_view = ''E'' AND NOT s.reported_by_exporter AND ge.id = s.exporter_id) AND
(COALESCE(sb.export_permit,'''') = COALESCE(s.export_permit_number,''''))
)
)
SELECT ARRAY(SELECT id FROM duplicates);
' INTO duplicates_ids;
RETURN duplicates_ids;
END;
$$;