forked from OHDSI/Vocabulary-v5.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_stage.sql
88 lines (83 loc) · 2.72 KB
/
load_stage.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
/**************************************************************************
* Copyright 2016 Observational Health Data Sciences and Informatics (OHDSI)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Authors: Timur Vakhitov, Christian Reich
* Date: 2017
**************************************************************************/
-- 1. Update latest_update field to new date
DO $_$
BEGIN
PERFORM VOCABULARY_PACK.SetLatestUpdate(
pVocabularyName => 'NFC',
pVocabularyDate => (SELECT vocabulary_date FROM sources.nfc LIMIT 1),
pVocabularyVersion => (SELECT vocabulary_version FROM sources.nfc LIMIT 1),
pVocabularyDevSchema => 'DEV_NFC'
);
END $_$;
-- 2. Truncate all working tables and remove indices
TRUNCATE TABLE concept_stage;
TRUNCATE TABLE concept_relationship_stage;
TRUNCATE TABLE concept_synonym_stage;
TRUNCATE TABLE pack_content_stage;
TRUNCATE TABLE drug_strength_stage;
--3. Insert into concept_stage
INSERT INTO concept_stage (
concept_name,
vocabulary_id,
domain_id,
concept_class_id,
standard_concept,
concept_code,
valid_start_date,
valid_end_date,
invalid_reason
)
SELECT concept_name,
'NFC' AS vocabulary_id,
'Drug' AS domain_id,
'NFC' AS concept_class_id,
'C' AS standard_concept,
concept_code,
TO_DATE('19700101', 'yyyymmdd') AS valid_start_date,
TO_DATE('20991231', 'yyyymmdd') AS valid_end_date,
NULL AS invalid_reason
FROM SOURCES.nfc;
--4. Add hierarchy inside NFC
INSERT INTO concept_relationship_stage (
concept_code_1,
concept_code_2,
relationship_id,
vocabulary_id_1,
vocabulary_id_2,
valid_start_date,
valid_end_date,
invalid_reason
)
SELECT uppr.concept_code AS concept_code_1,
lowr.concept_code AS concept_code_2,
'Is a' AS relationship_id,
'NFC' AS vocabulary_id_1,
'NFC' AS vocabulary_id_2,
v.latest_update AS valid_start_date,
TO_DATE('20991231', 'yyyymmdd') AS valid_end_date,
NULL AS invalid_reason
FROM concept_stage uppr,
concept_stage lowr,
vocabulary v
WHERE lowr.concept_code = SUBSTR(uppr.concept_code, 1, LENGTH(uppr.concept_code) - 1)
AND uppr.vocabulary_id = 'NFC'
AND lowr.vocabulary_id = 'NFC'
AND v.vocabulary_id = 'NFC';
-- At the end, the three tables concept_stage, concept_relationship_stage and concept_synonym_stage should be ready to be fed into the generic_update.sql script