|
| 1 | +-- |
| 2 | +-- Hub View definition for {{generationSpecificMetadata.selectedDataObject.name}} |
| 3 | +-- |
| 4 | +-- This template represents a standard Data Vault style 'Hub' table as a view. |
| 5 | +-- The view shows the same data as would otherwise be the case if the table would be created and all data logistics processes run. |
| 6 | +-- |
| 7 | + |
| 8 | +IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[{{metadataConfiguration.vdwSchemaName}}].[{{generationSpecificMetadata.selectedDataObject.name}}]') AND type in (N'V')) |
| 9 | +DROP VIEW [{{metadataConfiguration.vdwSchemaName}}].[{{generationSpecificMetadata.selectedDataObject.name}}] |
| 10 | +GO |
| 11 | + |
| 12 | +CREATE VIEW [{{metadataConfiguration.vdwSchemaName}}].[{{generationSpecificMetadata.selectedDataObject.name}}] AS |
| 13 | +SELECT {{#each dataObjectMappings}}{{#if @first}} |
| 14 | + {{#each businessKeys}} {{#if @first}}hub.[{{surrogateKey}}],{{/if}}{{/each}} |
| 15 | + hub.[{{../metadataConfiguration.etlProcessAttribute}}], |
| 16 | + hub.[{{../metadataConfiguration.loadDateTimeAttribute}}],{{#each businessKeys}}{{#each businessKeyComponentMapping}} |
| 17 | + hub.[{{targetDataItem.name}}],{{/each}}{{/each}} |
| 18 | + ROW_NUMBER() OVER ( ORDER BY |
| 19 | + [{{../metadataConfiguration.loadDateTimeAttribute}}],{{#each businessKeys}}{{#each businessKeyComponentMapping}} |
| 20 | + [{{targetDataItem.name}}]{{#unless @last}},{{/unless}}{{/each}}{{/each}} |
| 21 | + ) AS DATA_DELIVERY_KEY |
| 22 | + {{/if}}{{/each}} |
| 23 | +FROM ( |
| 24 | +{{#each dataObjectMappings}}{{#if @first}} |
| 25 | +SELECT |
| 26 | + HASHBYTES('MD5', {{#each businessKeys}} {{#if @first}} {{!-- Create the Surrogate Key using the Business Key and components --}} |
| 27 | + {{#each businessKeyComponentMapping}} |
| 28 | + ISNULL(RTRIM(CONVERT(NVARCHAR(100), {{targetDataItem.name}})), 'N/A') + '#~!'{{#unless @last}} +{{/unless}} |
| 29 | + {{/each}} |
| 30 | + ) AS {{surrogateKey}},{{/if}}{{/each}} |
| 31 | + -1 AS {{../metadataConfiguration.etlProcessAttribute}}, |
| 32 | + MIN({{../metadataConfiguration.loadDateTimeAttribute}}) AS {{../metadataConfiguration.loadDateTimeAttribute}}, |
| 33 | + --{{../metadataConfiguration.recordSourceAttribute}},{{#each businessKeys}} {{!-- The Hub business key --}} |
| 34 | + {{#each businessKeyComponentMapping}} |
| 35 | + {{targetDataItem.name}},{{/each}}{{/each}} |
| 36 | + ROW_NUMBER() OVER (PARTITION BY {{#each businessKeys}}{{#each businessKeyComponentMapping}} {{!-- Create a row number to enable selection only the earliest row, ordered by date/time --}} |
| 37 | + {{targetDataItem.name}}{{#unless @last}},{{/unless}}{{/each}} |
| 38 | + {{/each}} |
| 39 | + ORDER BY |
| 40 | + MIN({{../metadataConfiguration.loadDateTimeAttribute}}) |
| 41 | + ) AS ROW_NR |
| 42 | +FROM |
| 43 | +( {{/if}}{{/each}} |
| 44 | + {{#each dataObjectMappings }} |
| 45 | + SELECT {{#each businessKeys}}{{#each businessKeyComponentMapping}} |
| 46 | + CAST({{sourceDataItems.0.name}} AS NVARCHAR(100)) AS {{targetDataItem.name}},{{/each}}{{/each}} |
| 47 | + --{{../metadataConfiguration.recordSourceAttribute}}, |
| 48 | + MIN({{../metadataConfiguration.loadDateTimeAttribute}}) AS {{../metadataConfiguration.loadDateTimeAttribute}} |
| 49 | + FROM [{{sourceDataObjects.0.dataObjectConnection.extensions.0.value}}].[{{sourceDataObjects.0.dataObjectConnection.extensions.1.value}}].[{{sourceDataObjects.0.name}}] |
| 50 | + WHERE {{#each businessKeys}}{{#each businessKeyComponentMapping}} |
| 51 | + {{sourceDataItems.0.name}} IS NOT NULL{{#unless @last}} AND{{/unless}}{{/each}}{{/each}} |
| 52 | + {{#if filterCriterion}}AND {{filterCriterion}}{{/if}} |
| 53 | + GROUP BY |
| 54 | + {{#each businessKeys}}{{#each businessKeyComponentMapping}}{{#each sourceDataItems}}{{#stringcompare isHardCodedValue "True"}}{{else}}{{name}}{{#unless @last}},{{/unless}}{{/stringcompare}}{{/each}}{{/each}}{{/each}} |
| 55 | + -- {{../metadataConfiguration.recordSourceAttribute}} |
| 56 | + {{#unless @last}}UNION{{/unless}} |
| 57 | + {{/each}} |
| 58 | +) HUB_selection {{#each dataObjectMappings}}{{#if @first}} |
| 59 | +GROUP BY {{#each businessKeys}}{{#each businessKeyComponentMapping}} |
| 60 | + {{targetDataItem.name}}{{#unless @last}},{{/unless}}{{/each}}{{/each}} |
| 61 | + --{{../metadataConfiguration.recordSourceAttribute}} {{/if}}{{/each}} |
| 62 | +) hub |
| 63 | +WHERE ROW_NR = 1 |
| 64 | +UNION |
| 65 | +SELECT |
| 66 | + 0x00000000000000000000000000000000, |
| 67 | + - 1, |
| 68 | + '1900-01-01', |
| 69 | + --'Data Warehouse',{{#each dataObjectMappings}}{{#if @first}}{{#each businessKeys}}{{#each businessKeyComponentMapping}} |
| 70 | + 'Unknown',{{/each}}{{/each}}{{/if}}{{/each}} |
| 71 | + 0 AS DATA_DELIVERY_KEY |
0 commit comments