|
| 1 | +{# |
| 2 | + Copyright (c) 2024, Oracle and/or its affiliates. |
| 3 | + |
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | + |
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + |
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +#} |
| 16 | +{{ |
| 17 | + config( |
| 18 | + materialized='incremental', |
| 19 | + incremental_strategy='delete+insert', |
| 20 | + parallel=4, |
| 21 | + partition_config={"clause": "PARTITION BY HASH(PROD_NAME) PARTITIONS 4"}, |
| 22 | + table_compression_clause='COLUMN STORE COMPRESS FOR QUERY LOW') |
| 23 | +}} |
| 24 | + |
| 25 | +SELECT prod_name, channel_desc, calendar_month_desc, |
| 26 | + {{ snapshot_hash_arguments(['prod_name', 'channel_desc', 'calendar_month_desc']) }} AS group_id, |
| 27 | + TO_CHAR(SUM(amount_sold), '9,999,999,999') SALES$, |
| 28 | + RANK() OVER (ORDER BY SUM(amount_sold)) AS default_rank, |
| 29 | + RANK() OVER (ORDER BY SUM(amount_sold) DESC NULLS LAST) AS custom_rank |
| 30 | +FROM {{ source('sh_database', 'sales') }}, {{ source('sh_database', 'products') }}, {{ source('sh_database', 'customers') }}, |
| 31 | + {{ source('sh_database', 'times') }}, {{ source('sh_database', 'channels') }}, {{ source('sh_database', 'countries') }} |
| 32 | +WHERE sales.prod_id=products.prod_id AND sales.cust_id=customers.cust_id |
| 33 | + AND customers.country_id = countries.country_id AND sales.time_id=times.time_id |
| 34 | + AND sales.channel_id=channels.channel_id |
| 35 | + AND country_iso_code='US' |
| 36 | + |
| 37 | +{% if is_incremental() %} |
| 38 | + |
| 39 | + AND times.calendar_month_desc > (SELECT MAX(calendar_month_desc) FROM {{ this }}) |
| 40 | + |
| 41 | +{% endif %} |
| 42 | + |
| 43 | +GROUP BY prod_name, channel_desc, calendar_month_desc |
0 commit comments