-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIATIv203 - Data Population Stored Procedure.sql
More file actions
2847 lines (2527 loc) · 172 KB
/
IATIv203 - Data Population Stored Procedure.sql
File metadata and controls
2847 lines (2527 loc) · 172 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
USE [IATIv203]
GO
/****** Object: StoredProcedure [PublicationControl].[p_Populate] Script Date: 15/03/2020 10:53:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [PublicationControl].[p_Populate]
(
@ExportedFlag Configuration.Flag = 'N',
@VersionId Configuration.Version = NULL,
@PivotDays INT = 5,
@LatestTransactionDate DATETIME = NULL
)
AS
--*/
-- Uncomment these declarations if you want to run this routine as a script and not a procedure
DECLARE @ExportedFlagInternal Configuration.Flag
DECLARE @VersionIdInternal Configuration.Version
DECLARE @PivotDaysInternal INT
DECLARE @LatestTransactionDateInternal DATETIME
SET @ExportedFlagInternal = @ExportedFlag
SET @VersionIdInternal = @VersionId
SET @PivotDaysInternal = @PivotDays
SET @LatestTransactionDateInternal = @LatestTransactionDate
DECLARE @ActivitiesId INT
DECLARE @DFIDProjectIdentifier NVARCHAR(4)
DECLARE @DFIDOrganisationIdentifier NVARCHAR(8)
DECLARE @DFIDOrganisationType NVARCHAR(2)
DECLARE @DFIDOrganisationName NVARCHAR(60)
DECLARE @EarliestTransactionDate DATETIME
DECLARE @MinimumTransactionAmount INT
DECLARE @ComponentSource SYSNAME
DECLARE @DocumentSource SYSNAME
DECLARE @DocumentURIPrefix NVARCHAR(MAX)
SET @ActivitiesId = 101
SET @DFIDProjectIdentifier = N'GB-1'
SET @DFIDOrganisationIdentifier = N'GB-GOV-1'
SET @DFIDOrganisationType = N'10'
SET @DFIDOrganisationName = N'UK - Department for International Development (DFID)'
SET @EarliestTransactionDate = '20100512'
SET @MinimumTransactionAmount = 500
SET @ComponentSource = NULL
SET @DocumentSource = NULL
SET @DocumentURIPrefix = N'http://iati.dfid.gov.uk/iati_documents/'
SET @ExportedFlagInternal = ISNULL(@ExportedFlagInternal, 'N')
/* this allows the script to run without being part of the stored procedure if necessary,
* also the routine will not work if a NULL value is passed in the @PivotDaysInternal parameter*/
SET @PivotDaysInternal = ISNULL(@PivotDaysInternal, 5)
/* If no explicit latest transaction date is specified then we use the end of the previous month, provided we
* are at least @PivotDaysInternal + 1 days into the current month, otherwise we use the end of the month before that.
* With the default value of 5 days for @PivotDaysInternal then if it is on or after the 6th of the month we will use
* the end of the previous month, otherwise we will use the end of the month two months prior to the current month.*/
IF @LatestTransactionDateInternal IS NULL
BEGIN
DECLARE @PivotDateTime DATETIME
SET @PivotDateTime = DATEADD(DAY, -@PivotDaysInternal, GETDATE())
SET @LatestTransactionDateInternal = Configuration.f_MakeDate(DATEPART(YEAR, @PivotDateTime), DATEPART(MONTH, @PivotDateTime), 1) - 1
END
SET NOCOUNT ON
/* Storing Current Version from the DataWareHouse*/
IF @VersionIdInternal IS NULL
SET @VersionIdInternal = Configuration.f_CurrentVersion()
/* Storing the Population ID associated with current run of p_populate*/
DECLARE @Population TABLE
(
PopulationId INT NOT NULL
)
/* LastUpdatedDate changes for Project*/
DECLARE @ProjectLastUpdatedDate TABLE
(
ProjectId VARCHAR(25) NOT NULL,
LastUpdatedDate DATETIME NOT NULL
)
/* LastUpdatedDate changes for Component*/
DECLARE @ComponentLastUpdatedDate TABLE
(
ProjectId VARCHAR(25) NOT NULL,
ComponentId VARCHAR(25) NOT NULL,
LastUpdatedDate DATETIME NOT NULL
)
-- Used to hold project budget data per sector
DECLARE @ProjectBudgetBySector TABLE
(
ProjectId VARCHAR(25) NOT NULL,
SectorCode INT NOT NULL,
SectorText NVARCHAR(MAX) NOT NULL,
Budget FLOAT NOT NULL
)
-- Used to hold the percentage of project budget allocated per geographic location
DECLARE @ProjectBudgetByGeoLocation TABLE
(
ProjectId VARCHAR(25) NOT NULL,
BenefittingCountryCode VARCHAR(25) NULL,
CountryCode NCHAR(2) NULL,
RegionCode INT NULL,
Percentage DECIMAL(36,4) NOT NULL
)
-- Used to the hold Staging condition data for All Projects
DECLARE @StagedData TABLE
(
[iati-activityID] VARCHAR(25) ,
[HasSpecificConditions] VARCHAR(1) ,
[HasBudgetSupport] VARCHAR(1)
)
DBCC CHECKIDENT ('[PublicationControl].Population', RESEED, 0)
DBCC CHECKIDENT ('[PublicationControl].Population', RESEED)
BEGIN TRANSACTION
BEGIN TRY
/* Clearing down IATISchema tables before re-populating them */
DELETE FROM [IATISchema].[activity-date]
DELETE FROM [IATISchema].[budget]
DELETE FROM [IATISchema].[capital-spend]
DELETE FROM [IATISchema].[collaboration-type]
DELETE FROM [IATISchema].[conditions]
DELETE FROM [IATISchema].[conditions/condition]
DELETE FROM [IATISchema].[contact-info]
DELETE FROM [IATISchema].[contact-info/details]
DELETE FROM [IATISchema].[country-budget-items]
DELETE FROM [IATISchema].[country-budget-items/budget-item]
DELETE FROM [IATISchema].[crs-add]
DELETE FROM [IATISchema].[crs-add/channel-code]
DELETE FROM [IATISchema].[crs-add/loan-status]
DELETE FROM [IATISchema].[crs-add/loan-status/interest-arrears]
DELETE FROM [IATISchema].[crs-add/loan-status/interest-received]
DELETE FROM [IATISchema].[crs-add/loan-status/principal-arrears]
DELETE FROM [IATISchema].[crs-add/loan-status/principal-outstanding]
DELETE FROM [IATISchema].[crs-add/loan-terms]
DELETE FROM [IATISchema].[crs-add/loan-terms/commitment-date]
DELETE FROM [IATISchema].[crs-add/loan-terms/repayment-final-date]
DELETE FROM [IATISchema].[crs-add/loan-terms/repayment-first-date]
DELETE FROM [IATISchema].[crs-add/loan-terms/repayment-plan]
DELETE FROM [IATISchema].[crs-add/loan-terms/repayment-type]
DELETE FROM [IATISchema].[crs-add/other-flags]
DELETE FROM [IATISchema].[default-aid-type]
DELETE FROM [IATISchema].[description]
DELETE FROM [IATISchema].[document-link]
DELETE FROM [IATISchema].[document-link/category]
DELETE FROM [IATISchema].[document-link/description]
DELETE FROM [IATISchema].[document-link/document-date]
DELETE FROM [IATISchema].[document-link/language]
DELETE FROM [IATISchema].[document-link/title]
DELETE FROM [IATISchema].[humanitarian-scope]
DELETE FROM [IATISchema].[iati-activities]
DELETE FROM [IATISchema].[iati-activity]
DELETE FROM [IATISchema].[legacy-data]
DELETE FROM [IATISchema].[location]
DELETE FROM [IATISchema].[other-identifier]
DELETE FROM [IATISchema].[participating-org]
DELETE FROM [IATISchema].[policy-marker]
DELETE FROM [IATISchema].[recipient-country]
DELETE FROM [IATISchema].[recipient-region]
DELETE FROM [IATISchema].[related-activity]
DELETE FROM [IATISchema].[result]
DELETE FROM [IATISchema].[result/description]
DELETE FROM [IATISchema].[result/document-link]
DELETE FROM [IATISchema].[result/document-link/category]
DELETE FROM [IATISchema].[result/document-link/description]
DELETE FROM [IATISchema].[result/document-link/document-date]
DELETE FROM [IATISchema].[result/document-link/language]
DELETE FROM [IATISchema].[result/document-link/title]
DELETE FROM [IATISchema].[result/indicator]
DELETE FROM [IATISchema].[result/indicator/baseline]
DELETE FROM [IATISchema].[result/indicator/baseline/comment]
DELETE FROM [IATISchema].[result/indicator/baseline/dimension]
DELETE FROM [IATISchema].[result/indicator/baseline/document-link]
DELETE FROM [IATISchema].[result/indicator/baseline/document-link/category]
DELETE FROM [IATISchema].[result/indicator/baseline/document-link/description]
DELETE FROM [IATISchema].[result/indicator/baseline/document-link/document-date]
DELETE FROM [IATISchema].[result/indicator/baseline/document-link/language]
DELETE FROM [IATISchema].[result/indicator/baseline/document-link/title]
DELETE FROM [IATISchema].[result/indicator/baseline/location]
DELETE FROM [IATISchema].[result/indicator/document-link]
DELETE FROM [IATISchema].[result/indicator/document-link/category]
DELETE FROM [IATISchema].[result/indicator/document-link/description]
DELETE FROM [IATISchema].[result/indicator/document-link/document-date]
DELETE FROM [IATISchema].[result/indicator/document-link/language]
DELETE FROM [IATISchema].[result/indicator/document-link/title]
DELETE FROM [IATISchema].[result/indicator/period]
DELETE FROM [IATISchema].[result/indicator/period/actual]
DELETE FROM [IATISchema].[result/indicator/period/actual/comment]
DELETE FROM [IATISchema].[result/indicator/period/actual/dimension]
DELETE FROM [IATISchema].[result/indicator/period/actual/document-link]
DELETE FROM [IATISchema].[result/indicator/period/actual/document-link/category]
DELETE FROM [IATISchema].[result/indicator/period/actual/document-link/description]
DELETE FROM [IATISchema].[result/indicator/period/actual/document-link/document-date]
DELETE FROM [IATISchema].[result/indicator/period/actual/document-link/language]
DELETE FROM [IATISchema].[result/indicator/period/actual/location]
DELETE FROM [IATISchema].[result/indicator/period/period-end]
DELETE FROM [IATISchema].[result/indicator/period/period-start]
DELETE FROM [IATISchema].[result/indicator/period/target]
DELETE FROM [IATISchema].[result/indicator/period/target/comment]
DELETE FROM [IATISchema].[result/indicator/period/target/dimension]
DELETE FROM [IATISchema].[result/indicator/period/target/document-link]
DELETE FROM [IATISchema].[result/indicator/period/target/document-link/category]
DELETE FROM [IATISchema].[result/indicator/period/target/document-link/description]
DELETE FROM [IATISchema].[result/indicator/period/target/document-link/document-date]
DELETE FROM [IATISchema].[result/indicator/period/target/document-link/language]
DELETE FROM [IATISchema].[result/indicator/period/target/document-link/title]
DELETE FROM [IATISchema].[result/indicator/period/target/location]
DELETE FROM [IATISchema].[result/indicator/reference]
DELETE FROM [IATISchema].[result/indicator/title]
DELETE FROM [IATISchema].[result/reference]
DELETE FROM [IATISchema].[result/title]
DELETE FROM [IATISchema].[sector]
DELETE FROM [IATISchema].[tag]
DELETE FROM [IATISchema].[title]
DELETE FROM [IATISchema].[transaction]
DELETE FROM [IATISchema].[transaction/aid-type]
/* Storing Information for this run of p_populate */
INSERT INTO
[PublicationControl].Population
OUTPUT
INSERTED.PopulationId
INTO
@Population (PopulationId)
VALUES
(
@ExportedFlagInternal,
GETDATE(),
NULL,
@VersionIdInternal,
@ActivitiesId,
@DFIDOrganisationIdentifier,
@EarliestTransactionDate,
@LatestTransactionDateInternal,
@MinimumTransactionAmount,
@ComponentSource,
@DocumentSource
)
DECLARE @PopulationId INT
SET @PopulationId = (SELECT PopulationId FROM @Population)
/* Clear down the database table [PublicationControl].PopulationComponent */
DELETE FROM [PublicationControl].PopulationComponent
/* Store All Publishable Components to [PublicationControl].PopulationComponent */
IF @ComponentSource IS NULL
INSERT INTO [PublicationControl].PopulationComponent SELECT @PopulationId, ComponentCode, StatusFinData FROM [PublicationControl].[stageComponent]
ELSE
INSERT INTO [PublicationControl].PopulationComponent EXECUTE('SELECT ' + @PopulationId + ', * FROM (' + @ComponentSource + ') q')
/* Emergency response data should be added to [PublicationControl].PopulationComponent */
INSERT INTO [PublicationControl].PopulationComponent (PopulationId, ComponentId, StatusFinData)
SELECT @PopulationId, Eac.ComponentId, 'Release'
FROM [PublicationControl].EmergencyAidComponents Eac
WHERE (Select COUNT(*) From [PublicationControl].PopulationComponent Pc Where Eac.ComponentId = Pc.ComponentId AND @populationId = Pc.PopulationId ) < 1
/* Store Last Updated Date for Components */
EXECUTE [PublicationControl].p_PrintProgress N'Populating @ComponentLastUpdatedDate'
INSERT INTO
@ComponentLastUpdatedDate
SELECT LEFT(ComponentLastUpdate.ComponentId,6) AS ProjectId, ComponentLastUpdate.ComponentId, CONVERT(DATETIME,ComponentLastUpdate.UpdatedDate)
FROM
(
SELECT FullComponentList.ComponentId, MAX(LastUpdated) As UpdatedDate
FROM
(-- Return the last updated date of all components on days when there was not a system issue (i.e. a balance table problem) causing a huge number of updates
SELECT ip.ComponentId, MAX(DATEADD(d,-1,CONVERT(DATE,v.StartDateTime))) AS LastUpdated
FROM [ProjectDataMart].[AgressoTransformation].IATIKeyDataComponentTransformed ip
LEFT JOIN [ProjectDataMart].[Configuration].[Version] v ON ip.[FromVersionId] = v.[VersionId]
WHERE ip.FromVersionId NOT IN (SELECT VersionId FROM [ProjectDataMart].[Configuration].VersionedObjectRefreshHistory WHERE VersionedObjectId = 314 AND VersionedRows > 10000)
Group By ip.ComponentID
UNION ALL
-- Get the first date that every component appears within the table, unioning this table will return all components that were first created on one of the excluded versions (see above)
SELECT ip.ComponentId, MIN(DATEADD(d,-1,CONVERT(DATE,v.StartDateTime))) AS LastUpdated
FROM [ProjectDataMart].[AgressoTransformation].IATIKeyDataComponentTransformed ip
LEFT JOIN [ProjectDataMart].[Configuration].[Version] v ON ip.[FromVersionId] = v.[VersionId]
Group BY ip.ComponentID
UNION ALL
--Get the last updated date from the published transactions
SELECT ia.ComponentId, MAX(CONVERT(DATE,tr.[transaction-date/@iso-date])) LastUpdated
FROM [IATISchema].[transaction] tr
INNER JOIN
[IATISchema].[iati-activity] ia
ON
tr.[iati-activityID] = ia.[iati-activityID]
WHERE tr.[transaction-date/@iso-date] < GETDATE()
Group By ia.ProjectId, ia.ComponentId) FullComponentList
Group By FullComponentList.ComponentID
) ComponentLastUpdate
UPDATE
clu
SET
clu.LastUpdatedDate = vc.OperationalEndDate
From
@ComponentLastUpdatedDate clu
INNER JOIN
[PublicationControl].[v_Component] vc
ON
clu.ComponentId = vc.ComponentId
Where
clu.LastUpdatedDate='2012-04-05 00:00:00.000'
/* Store Last Updated Date for Projects */
EXECUTE [PublicationControl].p_PrintProgress N'Populating @ProjectLastUpdatedDate'
INSERT INTO
@ProjectLastUpdatedDate
SELECT ProjectLastUpdate.ProjectId, CONVERT(DATETIME,MAX(ProjectLastUpdate.LastUpdated)) AS LastUpdatedDate
FROM
( SELECT ip.ProjectId, DATEADD(d,-1,CONVERT(DATE,v.StartDateTime)) AS LastUpdated
FROM [ProjectDataMart].[AgressoTransformation].IATIKeyDataProjectTransformed ip
LEFT JOIN [ProjectDataMart].[Configuration].[Version] v ON ip.[FromVersionId] = v.[VersionId]
WHERE ip.ToVersionId IS NULL
UNION ALL
-- Get the Last updtaed dates for Published Documents
SELECT [ProjectID], MAX([ExtractionDate]) AS LastUpdated
FROM [PublicationControl].[PublishedDocuments]
-- Don't include the dates when all doc refferences were deleted from the table due to a system error
WHERE ExtractionDate NOT IN ('2014-04-06', '2014-04-24')
GROUP BY ProjectID
UNION ALL
-- Get the Last updtaed dates for Geo Locations
SELECT [ProjectID], MAX([LastUpdated])
--FROM (SELECT * FROM amp.[location].geocodes) as ampLocationGeocodes
FROM (SELECT * FROM [PublicationControl].[LocationData]) as ampLocationGeocodes
WHERE [Confirmed] = 1 AND Deleted = 0
GROUP BY ProjectID
UNION ALL
-- Get the Last updated dates for Geo Locations
SELECT ProjectId, MAX(LastUpdatedDate) AS LastUpdated
FROM @ComponentLastUpdatedDate
GROUP BY ProjectId
) ProjectLastUpdate
GROUP BY ProjectLastUpdate.ProjectId
UPDATE
plu
SET
plu.LastUpdatedDate = vp.OperationalEndDate
From
@ProjectLastUpdatedDate plu
INNER JOIN
[PublicationControl].[v_Project] vp
ON
plu.ProjectId = vp.ProjectId
Where
plu.LastUpdatedDate='2012-04-05 00:00:00.000'
/*Data Generation for [IATISchema] Tables starts here*/
BEGIN TRANSACTION
BEGIN TRY
SET NOCOUNT ON
/* Setting Variables for Data Generation*/
IF @VersionIdInternal IS NULL
SET @VersionIdInternal = Configuration.f_CurrentVersion()
DELETE FROM [IATISchema].[iati-activities] WHERE [iati-activitiesID] = @ActivitiesId
DELETE FROM [IATISchema].[iati-activity] WHERE [iati-activitiesID] = @ActivitiesId
DECLARE @GeneratedDateTime [IATISchema].[xsd:datetime]
SET @GeneratedDateTime = GETDATE()
IF @DFIDOrganisationIdentifier IS NULL
BEGIN
EXECUTE [PublicationControl].p_PrintProgress N'Extracting the DFID Organisation Identifier'
SET @DFIDOrganisationIdentifier = (SELECT ISNULL(Code, 'GB-GOV-1') FROM [Codelist].[OrganisationIdentifier] WHERE NAME='Department for International Development')
END
/* Store Meta-Data associated with p_populate run in [iati-activities] */
EXECUTE [PublicationControl].p_PrintProgress N'Populating iati-activities'
INSERT INTO
[IATISchema].[iati-activities]
VALUES
(
@ActivitiesId -- [iati-activitiesID]
,'Real Data' -- [Name]
,NULL -- [Notes]
,DEFAULT -- [@version]
,@GeneratedDateTime -- [@generated-datetime]
,DEFAULT -- [ir:registry-record/@xml:lang]
,@DFIDOrganisationIdentifier -- [ir:registry-record/@file-id]
,'http://dfid.gov.uk/projects/iati/activities.xml' -- [ir:registry-record/@source-url]
,@DFIDOrganisationIdentifier -- [ir:registry-record/@publisher-id]
,'Funding' -- [ir:registry-record/@publisher-role]
,'devtracker-feedback@dfid.gov.uk' -- [ir:registry-record/@contact-email]
,@DFIDOrganisationIdentifier -- [ir:registry-record/@donor-id]
,10 -- [ir:registry-record/@donor-type]
,NULL -- [ir:registry-record/@donor-country]
,'DFID Activity File' -- [ir:registry-record/@title]
,'All Periods' -- [ir:registry-record/@activity-period]
,@GeneratedDateTime -- [ir:registry-record/@last-updated-datetime]
,@GeneratedDateTime -- [ir:registry-record/@generated-datetime]
,1 -- [ir:registry-record/@verification-status]
,'application/xml' -- [ir:registry-record/@format]
,'IATI' -- [ir:registry-record/@license]
)
/* Store projects' data (e.g. level 1 IATI activities) in the [iati-activity] table*/
EXECUTE [PublicationControl].p_PrintProgress N'Populating iati-activity with projects'
EXECUTE [PublicationControl].p_PrintProgress N'@VersionIdInternal'
DECLARE @ProjectActivityMapping TABLE
(
[ProjectId] VARCHAR(25) NOT NULL PRIMARY KEY
,[iati-activityID] INT NOT NULL UNIQUE
)
INSERT INTO
[IATISchema].[iati-activity]
(
[iati-activitiesID]
,[ProjectId]
,[@hierarchy]
,[iati-identifier/text()]
,[other-identifier/@owner-ref]
,[other-identifier/@owner-name]
,[other-identifier/text()]
,[@last-updated-datetime]
)
OUTPUT
INSERTED.[ProjectId]
,INSERTED.[iati-activityID]
INTO
@ProjectActivityMapping
SELECT
@ActivitiesID AS [iati-activitiesID]
,v_ProjectTransformed.ProjectId AS [ProjectId]
,1 AS [@hierarchy],
CASE
WHEN v_ProjectTransformed.ProjectId LIKE '3%' THEN @DFIDOrganisationIdentifier + '-' + v_ProjectTransformed.ProjectId
ELSE @DFIDProjectIdentifier + '-' + v_ProjectTransformed.ProjectId
END AS [iati-identifier/text()]
,@DFIDOrganisationIdentifier
,@DFIDOrganisationName
,v_ProjectTransformed.ProjectId AS [ProjectIDText]
,plud.LastUpdatedDate
FROM
(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_ProjectTransformedCurrent) v_ProjectTransformed
INNER JOIN
@ProjectLastUpdatedDate plud
ON plud.ProjectId = v_ProjectTransformed.ProjectId
WHERE
v_ProjectTransformed.ProjectId IN
(
SELECT DISTINCT
ProjectId
FROM
(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_ProjectComponentMappingTransformedCurrent) v_ProjectComponentMappingTransformed
INNER JOIN
[PublicationControl].PopulationComponent Component
ON
Component.PopulationId = @PopulationId
AND v_ProjectComponentMappingTransformed.ComponentId = Component.ComponentId
WHERE
v_ProjectComponentMappingTransformed.ProjectFlag = 'Y'
AND v_ProjectComponentMappingTransformed.ComponentFlag = 'Y'
)
/* Store components' data (e.g. level 2 IATI activities) in the [iati-activity] table*/
EXECUTE [PublicationControl].p_PrintProgress N'Populating iati-activity with components'
DECLARE @ComponentActivityMapping TABLE
(
[ComponentId] VARCHAR(25) NOT NULL PRIMARY KEY
,[ProjectId] VARCHAR(25) NOT NULL
,[iati-activityID] INT NOT NULL UNIQUE
)
INSERT INTO
[IATISchema].[iati-activity]
(
[iati-activitiesID]
,[ProjectId]
,[ComponentId]
,[BenefittingCountryCode]
,[CountryCode]
,[RegionCode]
,[@hierarchy]
,[iati-identifier/text()]
,[other-identifier/@owner-ref]
,[other-identifier/@owner-name]
,[other-identifier/text()]
,[@last-updated-datetime]
)
OUTPUT
INSERTED.[ComponentId]
,INSERTED.[ProjectId]
,INSERTED.[iati-activityID]
INTO
@ComponentActivityMapping
SELECT
@ActivitiesID AS [iati-activitiesID]
,v_ComponentTransformed.ProjectId AS [ProjectId]
,v_ComponentTransformed.ComponentId AS [ComponentId]
,NULLIF(v_ComponentTransformed.BenefittingCountryCode, '') AS [BenefittingCountryCode]
,[MappingBenefittingCountry].IATICountryCode AS [CountryCode]
,[MappingBenefittingCountry].IATIRegionCode AS [RegionCode]
,2 AS [@hierarchy],
CASE
WHEN v_ComponentTransformed.ProjectId LIKE '3%' THEN @DFIDOrganisationIdentifier + '-' + v_ComponentTransformed.ComponentId
ELSE @DFIDProjectIdentifier + '-' + v_ComponentTransformed.ComponentId
END AS [iati-identifier/text()]
,@DFIDOrganisationIdentifier
,@DFIDOrganisationName
,v_ComponentTransformed.ComponentId AS [ComponentIDText]
,clud.LastUpdatedDate
FROM
(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_ComponentTransformedCurrent) v_ComponentTransformed
INNER JOIN
[PublicationControl].PopulationComponent Component
ON
Component.PopulationId = @PopulationId
AND v_ComponentTransformed.ComponentId = Component.ComponentId
INNER JOIN
@ComponentLastUpdatedDate clud
ON clud.ComponentId = v_ComponentTransformed.ComponentId
LEFT OUTER JOIN
[PublicationControl].[MappingBenefittingCountry]
ON
v_ComponentTransformed.BenefittingCountryCode = MappingBenefittingCountry.BenefittingCountryCode
/* Update every entry in the [iati-activity] table with reporting-org value */
EXECUTE [PublicationControl].p_PrintProgress N'Populating reporting-org'
UPDATE
[IATISchema].[iati-activity]
SET
[reporting-org/@xml:lang] = NULL
,[reporting-org/@ref] = @DFIDOrganisationIdentifier
,[reporting-org/@type] = 10 -- i.e. "Government" entry in OrganisationType code list
,[reporting-org/text()] = NULL -- text filled automatically by view if no explicit name specified
WHERE
[iati-activitiesID] = @ActivitiesId
/* Insert a valid OECD DAC region into the iati-activity table for activities that have a DFID specific Benefitting Country (i.e. a custom region)
* so that the activity meets the IATI 2.01 standard.
*/
EXECUTE [PublicationControl].p_PrintProgress N'Populating iati-activity components with OECD DAC regions where they have been allocated a DFID specific region'
UPDATE
ia
SET
ia.RegionCode = mr.DACRegionCode
From
[IATISchema].[iati-activity] ia
INNER JOIN
[PublicationControl].[MappingDFIDRegionToDACRegion] mr
ON
ia.BenefittingCountryCode = mr.BenefittingCountryCode
Where
ia.ComponentId IS NOT NULL
AND ia.BenefittingCountryCode IS NOT NULL
AND ia.RegionCode IS NULL
AND ia.CountryCode IS NULL
/* Store participating-org with funding and extending roles along with [iati-activityID] in [IATISchema].[participating-org]*/
EXECUTE [PublicationControl].p_PrintProgress N'Populating participating-org with funding, accountable and extending roles'
/* Funding, including identifying cross-government funds
Assumption: that funding is determined by component level relationships with budget centres.
Approach:
1. Identify component funding orgs using subheadcodes
2. Identify project-level funding orgs using aggregate of components
3. Insert a union of the 2 tables into the participating-org table
[JTA 28/12/2017]
*/
/* Drop Temporary objects */
IF OBJECT_ID('tempdb..#tempParticipatingOrgComponent', 'U') IS NOT NULL
DROP TABLE #tempParticipatingOrgComponent
IF OBJECT_ID('tempdb..#tempParticipatingOrgProject', 'U') IS NOT NULL
DROP TABLE #tempParticipatingOrgProject
/* 1. Identify component funding using subheadcodes and insert into temp table */
SELECT
a.[iati-activityID] AS [iati-activityID]
,a.ProjectId
,a.ComponentId
,NULL AS [@xml:lang]
,CASE
WHEN c.subheadcode is not null THEN s.[iati-orgcode]
ELSE 'GB-GOV-1' END AS [@ref]
,'10' AS [@type] -- Government
,'1' AS [@role] -- Funding
,CASE
WHEN c.subheadcode is not null THEN s.title
ELSE 'Department for International Development' END AS [text()]
INTO #tempParticipatingOrgComponent
FROM
[IATISchema].[iati-activity] a
LEFT JOIN
PublicationControl.v_CrossGov_Funds c on a.ComponentId = c.ComponentId
LEFT OUTER JOIN
PublicationControl.CrossGovFundsSubheads s on c.subheadcode = s.subheadcode
WHERE
a.[iati-activitiesID] = 101
and a.ComponentId is not NULL
/* 2. Identify project-level funding orgs using aggregate of components and insert into temp table */
SELECT DISTINCT
a.[iati-activityID] AS [iati-activityID]
,t.ProjectId
,null AS ComponentId
,NULL AS [@xml:lang]
,t.[@ref]
,t.[@type]
,t.[@role]
,t.[text()]
INTO #tempParticipatingOrgProject
FROM #tempParticipatingOrgComponent t
LEFT JOIN IATISchema.[iati-activity] a
on a.ProjectId = t.ProjectId
WHERE a.ComponentId is null
order by t.ProjectId
/* 3. Insert funding organisation results into participating-org table */
INSERT INTO
[IATISchema].[participating-org]
(
--[participating-orgID]
[iati-activityID]
,[@xml:lang]
,[@ref]
,[@type]
,[@role]
,[text()]
)
SELECT
[iati-activityID]
,[@xml:lang]
,[@ref]
,[@type]
,[@role]
,[text()]
FROM #tempParticipatingOrgProject
UNION
SELECT
[iati-activityID]
,[@xml:lang]
,[@ref]
,[@type]
,[@role]
,[text()]
FROM #tempParticipatingOrgComponent
/* Accountable organisations (in this case DFID only) */
INSERT INTO
[IATISchema].[participating-org]
(
--[participating-orgID]
[iati-activityID]
,[@xml:lang]
,[@ref]
,[@type]
,[@role]
,[text()]
)
SELECT
[iati-activity].[iati-activityID] AS [iati-activityID]
,NULL AS [@xml:lang]
,'GB-GOV-1' AS [@ref]
,'10' AS [@type] -- Government
,'2' AS [@role] -- Accountable
,'Department for International Development' AS [text()]
FROM
[IATISchema].[iati-activity]
WHERE
[iati-activity].[iati-activitiesID] = @ActivitiesId
/* Extending */
-- INSERT INTO
-- [IATISchema].[participating-org]
-- (
-- --[participating-orgID]
-- [iati-activityID]
-- ,[@xml:lang]
-- ,[@ref]
-- ,[@type]
-- ,[@role]
-- ,[text()]
-- )
-- SELECT
-- [iati-activity].[iati-activityID] AS [iati-activityID]
-- ,NULL AS [@xml:lang]
-- ,'GB-GOV-1' AS [@ref]
-- ,'10' AS [@type] -- Government
-- ,'3' AS [@role] -- Extending
-- ,'Department for International Development' AS [text()]
-- FROM
-- [IATISchema].[iati-activity]
-- WHERE
-- [iati-activity].[iati-activitiesID] = @ActivitiesId
/* Store recipient-country along with [iati-activityID] in [IATISchema].[other-identifier]*/
EXECUTE [PublicationControl].p_PrintProgress N'Populating other-identifier with Type A1'
INSERT INTO
[IATISchema].[other-identifier]
(
[iati-activityID]
,[@ref]
,[@type]
,[owner-org/@ref]
,[owner-org/text()]
)
SELECT
[iati-activity].[iati-activityID] AS [iati-activityID]
,[other-identifier/text()] AS [@ref]
,'A1' AS [@type]
,[other-identifier/@owner-ref] AS [owner-org/@ref]
,ISNULL([other-identifier/@owner-name], OtherIdentifierOwner.Name) AS [owner-org/text()]
FROM
[IATISchema].[iati-activity]
LEFT OUTER JOIN
[Codelist].OrganisationIdentifier OtherIdentifierOwner
ON
[iati-activity].[other-identifier/@owner-ref] = OtherIdentifierOwner.Code
WHERE
[iati-activity].[iati-activitiesID] = @ActivitiesId
EXECUTE [PublicationControl].p_PrintProgress N'Populating other-identifier with Type B1'
INSERT INTO
[IATISchema].[other-identifier]
(
[iati-activityID]
,[@ref]
,[@type]
,[owner-org/@ref]
,[owner-org/text()]
)
SELECT
[iati-activity].[iati-activityID] AS [iati-activityID]
,'GB-1' AS [@ref]
,'B1' AS [@type]
,[other-identifier/@owner-ref] AS [owner-org/@ref]
,'DFID previous reporting-org identifier' AS [owner-org/text()]
FROM
[IATISchema].[iati-activity]
LEFT OUTER JOIN
[Codelist].OrganisationIdentifier OtherIdentifierOwner
ON
[iati-activity].[other-identifier/@owner-ref] = OtherIdentifierOwner.Code
WHERE
[iati-activity].[iati-activitiesID] = @ActivitiesId
/* Store recipient-country along with [iati-activityID] in [IATISchema].[recipient-country]*/
EXECUTE [PublicationControl].p_PrintProgress N'Populating recipient-country'
INSERT INTO
[IATISchema].[recipient-country]
(
[iati-activityID]
,[@code]
,[@type]
,[@xml:lang]
,[@percentage]
,[text()]
)
SELECT
[iati-activity].[iati-activityID] AS [iati-activityID]
,[iati-activity].[CountryCode] AS [@code]
,NULL AS [@type]
,NULL AS [@xml:lang]
,NULL AS [@percentage]
,[MappingBenefittingCountry].[IATICountryName] AS [text()]
FROM
[IATISchema].[iati-activity]
LEFT OUTER JOIN
--(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_BenefittingCountryTransformed WHERE VersionId=@VersionIdInternal) v_BenefittingCountryTransformed
[PublicationControl].[MappingBenefittingCountry]
ON
[iati-activity].BenefittingCountryCode = [MappingBenefittingCountry].BenefittingCountryCode
WHERE
[iati-activity].[iati-activitiesID] = @ActivitiesId
AND [iati-activity].ComponentId IS NOT NULL
AND [iati-activity].CountryCode IS NOT NULL
/* Store IATI regions along with [iati-activityID] in [IATISchema].[recipient-region]*/
EXECUTE [PublicationControl].p_PrintProgress N'Populating recipient-region with IATI regions'
INSERT INTO
[IATISchema].[recipient-region]
(
[iati-activityID]
,[@code]
,[@type]
,[@xml:lang]
,[@percentage]
,[text()]
)
SELECT
[iati-activity].[iati-activityID] AS [iati-activityID]
,[iati-activity].[RegionCode] AS [@code]
,NULL AS [@type]
,NULL AS [@xml:lang]
,NULL AS [@percentage]
,NULL AS [text()]
FROM
[IATISchema].[iati-activity]
WHERE
[iati-activity].[iati-activitiesID] = @ActivitiesId
AND [iati-activity].ComponentId IS NOT NULL
AND [iati-activity].RegionCode IS NOT NULL
/*** Set the Default Aid Type based on the: Views, DAC sectors and Markers***/
INSERT INTO [IATISchema].[default-aid-type]
(
[iati-activityID]
,[@code]
,[@vocabulary]
)
SELECT
[iati-activityID] AS [iati-activityID]
,CASE
WHEN v_ComponentTransformed.FundingTypeCode = 'GENBUDGETSUPPORT' THEN 'A01'
WHEN v_ComponentTransformed.FundingTypeCode = 'SECTORBUDGETSUPPORT' THEN 'A02'
WHEN v_ComponentBiMultiMarkerTransformed.BiMultiMarkerCode = 3 THEN 'B01'
WHEN v_ComponentBiMultiMarkerTransformed.BiMultiMarkerCode = 2 THEN 'B02'
WHEN v_ComponentTransformed.FundingTypeCode = 'OTHERBILATERALDONOR' THEN 'B04'
WHEN v_ComponentTypeOfFinanceTransformed.TypeOfFinanceCode IN ('610', '611', '612', '613', '614', '615', '616', '617', '618') THEN 'F01'
WHEN sector.DACSectorCode = '91010' THEN 'G01'
WHEN sector.DACSectorCode = '99820' THEN 'H01'
WHEN v_ComponentBiMultiMarkerTransformed.BiMultiMarkerCode = 1 THEN 'C01'
END AS [@code]
,'1' AS [@vocabulary]
FROM
[IATISchema].[iati-activity]
LEFT OUTER JOIN
(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_ComponentTransformedCurrent) v_ComponentTransformed
ON
[iati-activity].ComponentId = v_ComponentTransformed.ComponentId
LEFT OUTER JOIN
(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_ComponentBiMultiMarkerTransformedCurrent) v_ComponentBiMultiMarkerTransformed
ON
[iati-activity].ComponentId = v_ComponentBiMultiMarkerTransformed.ComponentId
LEFT OUTER JOIN
(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_ComponentTypeOfFinanceTransformedCurrent) v_ComponentTypeOfFinanceTransformed
ON
v_ComponentTypeOfFinanceTransformed.Rank = 1
AND [iati-activity].ComponentId = v_ComponentTypeOfFinanceTransformed.ComponentId
LEFT OUTER JOIN
(
SELECT
v_ComponentInputSectorTransformed.ComponentId
,v_InputSectorTransformed.DACSectorCode
,ROW_NUMBER() OVER (PARTITION BY v_ComponentInputSectorTransformed.ComponentId ORDER BY SUM(Percentage) DESC) AS Rank
FROM
(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_ComponentInputSectorTransformedCurrent) v_ComponentInputSectorTransformed
INNER JOIN
(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_InputSectorTransformedCurrent) v_InputSectorTransformed
ON
v_ComponentInputSectorTransformed.InputSectorCode = v_InputSectorTransformed.InputSectorCode
GROUP BY
v_ComponentInputSectorTransformed.ComponentId
,v_InputSectorTransformed.DACSectorCode
) sector
ON
sector.Rank = 1
AND [iati-activity].ComponentId = sector.ComponentId
WHERE
[iati-activity].[iati-activitiesID] = @ActivitiesId
AND [iati-activity].ComponentId IS NOT NULL
/*** collaboration-type ***/
UPDATE
[iati-activity]
SET
[collaboration-type/@xml:lang] = NULL
,[collaboration-type/@code] =
CASE
WHEN v_ComponentBiMultiMarkerTransformed.BiMultiMarkerCode = 1 AND LEFT(v_ComponentTransformed.ChannelCode, 1) = '2' THEN 3
WHEN v_ComponentBiMultiMarkerTransformed.BiMultiMarkerCode = 3 AND LEFT(v_ComponentTransformed.ChannelCode, 1) = '2' THEN 3
WHEN v_ComponentBiMultiMarkerTransformed.BiMultiMarkerCode = 1 AND LEFT(v_ComponentTransformed.ChannelCode, 1) = '3' THEN 3
WHEN v_ComponentBiMultiMarkerTransformed.BiMultiMarkerCode = 1 THEN 1
WHEN v_ComponentBiMultiMarkerTransformed.BiMultiMarkerCode = 2 THEN 2
END
,[collaboration-type/@type] = NULL
,[collaboration-type/text()] = NULL
FROM
[IATISchema].[iati-activity]
LEFT OUTER JOIN
(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_ComponentTransformedCurrent) v_ComponentTransformed
ON
[iati-activity].ComponentId = v_ComponentTransformed.ComponentId
LEFT OUTER JOIN
(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_ComponentBiMultiMarkerTransformedCurrent) v_ComponentBiMultiMarkerTransformed
ON
[iati-activity].ComponentId = v_ComponentBiMultiMarkerTransformed.ComponentId
WHERE
[iati-activity].[iati-activitiesID] = @ActivitiesId
AND [iati-activity].ComponentId IS NOT NULL
/*** default-flow-type ***/
EXECUTE [PublicationControl].p_PrintProgress N'Populating default-flow-type'
UPDATE
[iati-activity]
SET
[default-flow-type/@xml:lang] = NULL
,[default-flow-type/@code] =
CASE
WHEN v_ComponentTransformed.ODAOOFMarkerCode = 'ODA' THEN '10'
WHEN v_ComponentTransformed.ODAOOFMarkerCode = 'OOF' THEN '20'
END
,[default-flow-type/@type] = NULL
,[default-flow-type/text()] = NULL
FROM
[IATISchema].[iati-activity]
LEFT OUTER JOIN
(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_ComponentTransformedCurrent) v_ComponentTransformed
ON
[iati-activity].ComponentId = v_ComponentTransformed.ComponentId
WHERE
[iati-activity].[iati-activitiesID] = @ActivitiesId
AND [iati-activity].ComponentId IS NOT NULL
/*** default-finance-type ***/
UPDATE
[iati-activity]
SET
[default-finance-type/@xml:lang] = NULL
,[default-finance-type/@code] = v_ComponentTypeOfFinanceTransformed.TypeOfFinanceCode
,[default-finance-type/@type] = NULL
,[default-finance-type/text()] = NULL
FROM
[IATISchema].[iati-activity]
LEFT OUTER JOIN
(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_ComponentTransformedCurrent) v_ComponentTransformed
ON
[iati-activity].ComponentId = v_ComponentTransformed.ComponentId
LEFT OUTER JOIN
(SELECT * FROM [ProjectDataMart].AgressoTransformation.v_ComponentTypeOfFinanceTransformedCurrent) v_ComponentTypeOfFinanceTransformed
ON
v_ComponentTypeOfFinanceTransformed.Rank = 1
AND [iati-activity].ComponentId = v_ComponentTypeOfFinanceTransformed.ComponentId
INNER JOIN
[Codelist].FinanceType
ON
v_ComponentTypeOfFinanceTransformed.TypeOfFinanceCode = FinanceType.Code
WHERE
[iati-activity].[iati-activitiesID] = @ActivitiesId
AND [iati-activity].ComponentId IS NOT NULL
/* other-identifier */
-- No Action
/* Populate Project Title along with [iati-activityID] in [IATISchema].[title]*/
EXECUTE [PublicationControl].p_PrintProgress N'Populating title for projects and components'
INSERT INTO
[IATISchema].[title]