-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreate_functions.sql
4145 lines (3363 loc) · 103 KB
/
create_functions.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
-- noinspection SqlDialectInspectionForFile
/*
This file contains common functions that are useful writing reports
For documentation on available functions, please see the sql_function_reference.csv file in this directory
*/
-- You should uncomment this line to check syntax in IDE. Liquibase handles this internally.
-- DELIMITER #
/*
get concept_id from report_mapping table
*/
#
DROP FUNCTION IF EXISTS concept_from_mapping;
#
CREATE FUNCTION concept_from_mapping(
_source varchar(50),
_code varchar(255)
)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE mappedConcept INT;
SELECT concept_id INTO mappedConcept FROM report_mapping WHERE source = _source and code = _code;
RETURN mappedConcept;
END
#
/*
get names from the concept_name table
*/
#
DROP FUNCTION IF EXISTS concept_name;
#
CREATE FUNCTION concept_name(
_conceptID INT,
_locale varchar(50)
)
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE conceptName varchar(255);
SELECT name INTO conceptName
FROM concept_name
WHERE voided = 0
AND concept_id = _conceptID
order by if(_locale = locale, 0, 1), if(locale = 'en', 0, 1),
locale_preferred desc, ISNULL(concept_name_type) asc,
field(concept_name_type,'FULLY_SPECIFIED','SHORT')
limit 1;
RETURN conceptName;
END
#
/*
get short name from the concept_name table
*/
#
DROP FUNCTION IF EXISTS concept_short_name;
#
CREATE FUNCTION concept_short_name(
_conceptID INT,
_locale varchar(50)
)
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE conceptName varchar(255);
SELECT name INTO conceptName
FROM concept_name
WHERE voided = 0
AND concept_id = _conceptID
order by if(_locale = locale, 0, 1), if(locale = 'en', 0, 1),
field(concept_name_type,'FULLY_SPECIFIED','SHORT') desc,
locale_preferred desc, ISNULL(concept_name_type) asc
limit 1;
RETURN conceptName;
END
#
/*
returns person name of user_id
*/
#
DROP FUNCTION IF EXISTS person_name_of_user;
#
CREATE FUNCTION person_name_of_user(
_user_id int
)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE personName TEXT;
select concat(given_name, ' ', family_name) into personName
from person_name pn
inner join users u on u.person_id = pn.person_id and u.user_id = _user_id
where voided = 0
order by pn.preferred desc, pn.date_created desc
limit 1;
RETURN personName;
END
#
/*
return encounter type id given name or uuid
*/
#
DROP FUNCTION IF EXISTS encounter_type;
#
CREATE FUNCTION encounter_type(
_name_or_uuid varchar(255)
)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE ret varchar(255);
SELECT encounter_type_id INTO ret
FROM encounter_type where name = _name_or_uuid or uuid = _name_or_uuid;
RETURN ret;
END
#
-- The following function accepts an encounter_type_id
-- It will return the names of the encounter_type
#
DROP FUNCTION IF EXISTS encounter_type_name_from_id;
#
CREATE FUNCTION encounter_type_name_from_id(
_encounter_type_id INT
)
RETURNS varchar(50)
DETERMINISTIC
BEGIN
DECLARE encounterName varchar(50);
SELECT
et.name INTO encounterName
FROM
encounter_type et
where encounter_type_id = _encounter_type_id;
RETURN encounterName;
END
#
/*
return patient identifier type id
*/
#
DROP FUNCTION IF EXISTS patient_identifier;
#
CREATE FUNCTION patient_identifier(
_patient_id int,
_name_or_uuid varchar(255)
)
RETURNS varchar(50)
DETERMINISTIC
BEGIN
DECLARE ret varchar(50);
SELECT i.identifier INTO ret
FROM patient_identifier i
INNER JOIN patient_identifier_type t on i.identifier_type = t.patient_identifier_type_id
WHERE (t.name = _name_or_uuid or t.uuid = _name_or_uuid)
AND i.voided = 0
AND i.patient_id = _patient_id
ORDER BY preferred desc, i.date_created desc limit 1;
RETURN ret;
END
#
/*
This function accepts patient/person id and returns that person's age in years
*/
#
DROP FUNCTION IF EXISTS current_age_in_years;
#
CREATE FUNCTION current_age_in_years(
_person_id int)
RETURNS int
DETERMINISTIC
BEGIN
DECLARE currentAge int;
select TIMESTAMPDIFF(YEAR, birthdate, now()) into currentAge
from person p
where p.person_id = _person_id;
RETURN currentAge;
END
#
/*
This function accepts patient/person id and returns that person's age in months
*/
#
DROP FUNCTION IF EXISTS current_age_in_months;
#
CREATE FUNCTION current_age_in_months(
_person_id int)
RETURNS DOUBLE
DETERMINISTIC
BEGIN
DECLARE currentAge DOUBLE;
select TIMESTAMPDIFF(MONTH, birthdate, now()) into currentAge
from person p
where p.person_id = _person_id;
RETURN currentAge;
END
#
/*
get patient age at encounter
*/
#
DROP FUNCTION IF EXISTS age_at_enc;
#
CREATE FUNCTION age_at_enc(
_person_id int,
_encounter_id int
)
RETURNS DOUBLE
DETERMINISTIC
BEGIN
DECLARE ageAtEnc DOUBLE;
select TIMESTAMPDIFF(YEAR, birthdate, encounter_datetime) into ageAtENC
from encounter e
join person p on p.person_id = e.patient_id
where e.encounter_id = _encounter_id
and p.person_id = _person_id;
RETURN ageAtEnc;
END
#
/*
get patient EMR ZL
*/
#
DROP FUNCTION IF EXISTS zlemr;
#
CREATE FUNCTION zlemr(
_patient_id int
)
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE zlEMR VARCHAR(255);
SELECT patient_identifier(_patient_id, 'ZL EMR ID') into zlEMR;
RETURN zlEMR;
END
#
DROP FUNCTION IF EXISTS dosId;
#
CREATE FUNCTION dosId (patient_id_in int(11))
RETURNS varchar(50)
DETERMINISTIC
BEGIN
DECLARE dosId_out varchar(50);
select identifier into dosId_out
from patient_identifier pid
where pid.patient_id = patient_id_in
and pid.voided = 0
and pid.identifier_type =
(select patient_identifier_type_id from patient_identifier_type where uuid = 'e66645eb-03a8-4991-b4ce-e87318e37566')
order by pid.preferred desc, pid.date_created asc limit 1
;
RETURN dosId_out;
END;
#
/*
unknown patient
*/
#
DROP FUNCTION IF EXISTS unknown_patient;
#
CREATE FUNCTION unknown_patient(
_patient_id int
)
RETURNS VARCHAR(50)
DETERMINISTIC
BEGIN
DECLARE unknownPatient VARCHAR(50);
select person_id into unknownPatient from person_attribute where person_attribute_type_id = (select person_attribute_type_id from
person_attribute_type where name = 'Unknown patient') and voided = 0 and person_id = _patient_id;
RETURN unknownPatient;
END
#
/*
person_attribute_value
*/
#
DROP FUNCTION IF EXISTS person_attribute_value;
#
CREATE FUNCTION person_attribute_value(
_patient_id int,
_att_type_name varchar(50)
)
RETURNS VARCHAR(50)
DETERMINISTIC
BEGIN
DECLARE attVal VARCHAR(50);
select a.value into attVal
from person_attribute a
inner join person_attribute_type pat on a.person_attribute_type_id = pat.person_attribute_type_id
where pat.name = _att_type_name
and a.voided = 0
and a.person_id = _patient_id
order by a.date_created desc
limit 1;
RETURN attVal;
END
#
/*
gender
*/
#
DROP FUNCTION IF EXISTS gender;
#
CREATE FUNCTION gender(
_patient_id int
)
RETURNS VARCHAR(50)
DETERMINISTIC
BEGIN
DECLARE patientGender VARCHAR(50);
select gender into patientGender from person where person_id = _patient_id and voided =0;
RETURN patientGender;
END
#
/*
patient address
*/
#
DROP FUNCTION IF EXISTS person_address;
#
CREATE FUNCTION person_address(
_patient_id int
)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE patientAddress TEXT;
select concat(IFNULL(state_province,''), ',' ,IFNULL(city_village,''), ',', IFNULL(address3,''), ',', IFNULL(address1,''), ',',IFNULL(address2,'')) into patientAddress
from person_address where voided = 0 and person_id = _patient_id order by preferred desc, date_created desc limit 1;
RETURN patientAddress;
END
#
/*
patient name
*/
#
DROP FUNCTION IF EXISTS person_name;
#
CREATE FUNCTION person_name(
_person_id int
)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE personName TEXT;
select concat(given_name, ' ', family_name) into personName
from person_name
where voided = 0
and person_id = _person_id
order by preferred desc, date_created desc
limit 1;
RETURN personName;
END
#
/*
patient birthdate
*/
#
DROP FUNCTION IF EXISTS birthdate;
#
CREATE FUNCTION birthdate(
_patient_id int
)
RETURNS date
DETERMINISTIC
BEGIN
DECLARE patientBirthdate date;
select birthdate into patientBirthdate from person where person_id = _patient_id;
RETURN patientBirthdate;
END
#
/*
patient GIVEN name
*/
#
DROP FUNCTION IF EXISTS person_given_name;
#
CREATE FUNCTION person_given_name(
_person_id int
)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE personGivenName TEXT;
select given_name into personGivenName
from person_name
where voided = 0
and person_id = _person_id
order by preferred desc, date_created desc
limit 1;
RETURN personGivenName;
END
#
/*
patient FAMILY name
*/
#
DROP FUNCTION IF EXISTS person_family_name;
#
CREATE FUNCTION person_family_name(
_person_id int
)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE personFamilyName TEXT;
select family_name into personFamilyName
from person_name
where voided = 0
and person_id = _person_id
order by preferred desc, date_created desc
limit 1;
RETURN personFamilyName;
END
#
/*
person middle name
*/
#
DROP FUNCTION IF EXISTS person_middle_name;
#
CREATE FUNCTION person_middle_name(
_person_id int
)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE personMiddleName TEXT;
select middle_name into personMiddleName
from person_name
where voided = 0
and person_id = _person_id
order by preferred desc, date_created desc
limit 1;
RETURN personMiddleName;
END
#
/*
ZL EMR ID location
*/
#
DROP FUNCTION IF EXISTS loc_registered;
#
CREATE FUNCTION loc_registered(
_patient_id int
)
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE locRegistered varchar(255);
select name into locRegistered from location l join patient_identifier pi on pi.location_id = l.location_id and pi.voided = 0 and pi.patient_id = _patient_id
and identifier_type = (select pid2.patient_identifier_type_id from patient_identifier_type pid2 where
pid2.uuid = metadata_uuid('org.openmrs.module.emrapi', 'emr.primaryIdentifierType')) limit 1;
RETURN locRegistered;
END
#
/*
returns the registration date of the given patient
*/
#
DROP FUNCTION IF EXISTS registration_date;
#
CREATE FUNCTION registration_date(
_patient_id int
)
RETURNS DATETIME
DETERMINISTIC
BEGIN
DECLARE return_date datetime;
select encounter_datetime into return_date from encounter
where patient_id = _patient_id
and encounter_type = (select encounter_type_id from encounter_type et where uuid = '873f968a-73a8-4f9c-ac78-9f4778b751b6')
and voided = 0
limit 1
;
RETURN return_date;
END
#
/*
Provider
*/
#
DROP FUNCTION IF EXISTS provider;
#
CREATE FUNCTION provider (
_encounter_id int
)
RETURNS varchar(255)
DETERMINISTIC
BEGIN
DECLARE providerName varchar(255);
select CONCAT(given_name, ' ', family_name) into providerName
from person_name pn join provider pv on pn.person_id = pv.person_id AND pn.voided = 0
join encounter_provider ep on pv.provider_id = ep.provider_id and ep.voided = 0 and ep.encounter_id = _encounter_id
limit 1;
RETURN providerName;
END
#
-- This function accepts encounter_id, provider_type, and offset
-- It return the name of the nth provider (based of offset) or the specified type
#
DROP FUNCTION IF EXISTS provider_name_of_type;
#
CREATE FUNCTION provider_name_of_type (
_encounter_id int,
_provider_type varchar(255),
_offset_value int
)
RETURNS varchar(255)
DETERMINISTIC
BEGIN
DECLARE providerName varchar(255);
select CONCAT(given_name, ' ', family_name) into providerName
from person_name pn join provider pv on pn.person_id = pv.person_id AND pn.voided = 0
inner join encounter_provider ep on pv.provider_id = ep.provider_id and ep.voided = 0 and ep.encounter_id = _encounter_id
and ep.encounter_role_id = _provider_type
order by ep.date_created desc, ep.encounter_provider_id desc
limit 1 offset _offset_value;
RETURN providerName;
END
#
/*
This function accepts an encounter_id
It will return the text of provider type for that encounter
NOTE that this will only return one provider type and should not be used if there are multiple providers on an encounter
*/
#
DROP FUNCTION IF EXISTS provider_type;
#
CREATE FUNCTION provider_type (
_encounter_id int
)
RETURNS varchar(255)
DETERMINISTIC
BEGIN
DECLARE providerType varchar(255);
select pr.name into providerType
from providermanagement_provider_role pr
join provider pv on pr.provider_role_id = pv.provider_role_id
join encounter_provider ep on pv.provider_id = ep.provider_id and ep.voided = 0 and ep.encounter_id = _encounter_id
limit 1;
RETURN providerType;
END
#
-- This function accepts an encounter_id
-- It will return the date created of that encounter
#
DROP FUNCTION IF EXISTS encounter_date_created;
#
CREATE FUNCTION encounter_date_created (
_encounter_id int
)
RETURNS datetime
DETERMINISTIC
BEGIN
DECLARE dateCreated datetime;
select e.date_created into dateCreated
from encounter e
where e.encounter_id = _encounter_id;
RETURN dateCreated;
END
#
-- This function accepts an encounter_id
-- It will return the name of the creator of that encounter
#
DROP FUNCTION IF EXISTS encounter_creator_name;
#
CREATE FUNCTION encounter_creator_name (
_encounter_id int
)
RETURNS VARCHAR(100)
DETERMINISTIC
BEGIN
DECLARE creator VARCHAR(100);
select person_name_of_user(e.creator) into creator
from encounter e
where encounter_id = _encounter_id;
/*select concat(given_name, ' ', family_name) into creator
from encounter e
inner join users u on e.creator = u.user_id
inner join person_name pn on pn.person_id = u.person_id
where e.encounter_id = _encounter_id;*/
RETURN creator;
END
#
/*
returns provider name of provider_id
*/
#
DROP FUNCTION IF EXISTS provider_name_from_provider_id;
#
CREATE FUNCTION provider_name_from_provider_id(
_provider_id int
)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE personName TEXT;
select concat(given_name, ' ', family_name) into personName
from provider p
INNER JOIN person_name pn ON p.person_id =pn.person_id AND p.provider_id= _provider_id
INNER JOIN users u on u.person_id = pn.person_id
where voided = 0
order by pn.preferred desc, pn.date_created desc
limit 1;
RETURN personName;
END
#
/*
Encounter Location
*/
#
DROP FUNCTION IF EXISTS encounter_location_name;
#
CREATE FUNCTION encounter_location_name (
_encounter_id int
)
RETURNS varchar(255)
DETERMINISTIC
BEGIN
DECLARE locName varchar(255);
select l.name into locName
from encounter e
inner join location l on l.location_id = e.location_id
where e.encounter_id = _encounter_id;
RETURN locName;
END
#
/*
Encounter Parent Location
It accepts encounter id and returns the parent location of the encounter.
It looks 3 levels deep - the encounter location, that location's parent and then that location's parent.
The first location it finds without another parent is returned.
*/
#
DROP FUNCTION IF EXISTS encounter_parent_location_name;
#
CREATE FUNCTION encounter_parent_location_name (
_encounter_id int
)
RETURNS varchar(255)
DETERMINISTIC
BEGIN
DECLARE locName varchar(255);
select if(l.parent_location is null, l.name, if(lp.parent_location is null, lp.name, lp2.name)) into locName
from encounter e
inner join location l on l.location_id = e.location_id
left outer join location lp on lp.location_id = l.parent_location
left outer join location lp2 on lp2.location_id = lp.parent_location
where e.encounter_id = _encounter_id;
RETURN locName;
END
#
/*
Encounter Date
*/
#
DROP FUNCTION IF EXISTS encounter_date;
#
CREATE FUNCTION encounter_date (
_encounter_id int
)
RETURNS datetime
DETERMINISTIC
BEGIN
DECLARE encDate datetime;
select e.encounter_datetime into encDate
from encounter e
where e.encounter_id = _encounter_id;
RETURN encDate;
END
#
/*
Visit date
*/
#
DROP FUNCTION IF EXISTS visit_date;
#
CREATE FUNCTION visit_date(
_encounter_id int
)
RETURNS DATE
DETERMINISTIC
BEGIN
DECLARE visitDate date;
select date(date_started) into visitDate from visit where voided = 0 and visit_id = (select visit_id from encounter where encounter_id = _encounter_id);
RETURN visitDate;
END
#
/*
This function accepts an obs_id and returns the obs_datetime of that obs
*/
#
DROP FUNCTION IF EXISTS obs_date;
#
CREATE FUNCTION obs_date (
_obs_id int
)
RETURNS datetime
DETERMINISTIC
BEGIN
DECLARE obsDate datetime;
select o.obs_datetime into obsDate
from obs o
where o.obs_id = _obs_id;
RETURN obsDate;
END
#
/*
Program
*/
#
DROP FUNCTION IF EXISTS program;
#
CREATE FUNCTION program(_name varchar (255))
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE programId int;
select program_id into programId from program where retired = 0 and name = _name;
RETURN programId;
END
#
/*
Relationship
*/
#
DROP FUNCTION IF EXISTS relation_type;
#
CREATE FUNCTION relation_type(
_name VARCHAR(255)
)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE relationshipID INT;
SELECT relationship_type_id INTO relationshipID FROM relationship_type WHERE retired = 0 AND a_is_to_b = _name;
RETURN relationshipID;
END
#
DROP FUNCTION IF EXISTS person_address_country;
#
CREATE FUNCTION person_address_country(
_patient_id int
)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE patientAddressCountry TEXT;
select country into patientAddressCountry
from person_address where voided = 0 and person_id = _patient_id order by preferred desc, date_created desc limit 1;
RETURN patientAddressCountry;
END
#
/*
patient address
*/
#
DROP FUNCTION IF EXISTS person_address_state_province;
#
CREATE FUNCTION person_address_state_province(
_patient_id int
)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE patientAddressStateProvince TEXT;
select state_province into patientAddressStateProvince
from person_address where voided = 0 and person_id = _patient_id order by preferred desc, date_created desc limit 1;
RETURN patientAddressStateProvince;
END
#
#
DROP FUNCTION IF EXISTS person_address_city_village;
#
CREATE FUNCTION person_address_city_village(
_patient_id int
)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE patientAddressCityVillage TEXT;
select city_village into patientAddressCityVillage
from person_address where voided = 0 and person_id = _patient_id order by preferred desc, date_created desc limit 1;
RETURN patientAddressCityVillage;
END
#
#
DROP FUNCTION IF EXISTS person_address_three;
#
CREATE FUNCTION person_address_three(
_patient_id int
)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE patientAddressThree TEXT;
select address3 into patientAddressThree
from person_address where voided = 0 and person_id = _patient_id order by preferred desc, date_created desc limit 1;
RETURN patientAddressThree;
END
#
#
DROP FUNCTION IF EXISTS person_address_one;
#
CREATE FUNCTION person_address_one(
_patient_id int
)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE patientAddressOne TEXT;
select address1 into patientAddressOne
from person_address where voided = 0 and person_id = _patient_id order by preferred desc, date_created desc limit 1;
RETURN patientAddressOne;
END
#
#
DROP FUNCTION IF EXISTS person_address_two;
#
CREATE FUNCTION person_address_two(
_patient_id int
)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE patientAddressTwo TEXT;
select address2 into patientAddressTwo
from person_address where voided = 0 and person_id = _patient_id order by preferred desc, date_created desc limit 1;
RETURN patientAddressTwo;
END
#
/*
patient address - country district component
*/
#
DROP FUNCTION IF EXISTS person_address_county_district;
#
CREATE FUNCTION person_address_county_district(
_patient_id int
)