diff --git a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_daily_summary.sql b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_daily_summary.sql index e52d8060f3..c0da19b59d 100644 --- a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_daily_summary.sql +++ b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_daily_summary.sql @@ -112,10 +112,10 @@ FROM ( sum(savingsplan_savingsplaneffectivecost) as savingsplan_effective_cost, sum( CASE - WHEN lineitem_lineitemtype='Tax' - OR lineitem_lineitemtype='Usage' - THEN lineitem_unblendedcost - ELSE savingsplan_savingsplaneffectivecost + WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' + OR lineitem_lineitemtype='SavingsPlanNegation' + THEN savingsplan_savingsplaneffectivecost + ELSE lineitem_unblendedcost END ) as calculated_amortized_cost, sum(pricing_publicondemandcost) as public_on_demand_cost, diff --git a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute_p.sql b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute_p.sql index ab5d111d6d..4e8c20f218 100644 --- a/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute_p.sql +++ b/koku/masu/database/trino_sql/reporting_awscostentrylineitem_summary_by_ec2_compute_p.sql @@ -112,58 +112,58 @@ SELECT uuid() as uuid, aa.id as account_alias_id FROM ( SELECT min(date(lineitem_usagestartdate)) as usage_start, - max(date(lineitem_usagestartdate)) as usage_end, - max(lineitem_usageaccountid) as usage_account_id, - lineitem_resourceid as resource_id, - max(nullif(product_operatingsystem, '')) as operating_system, - max(nullif(product_region, '')) as region, - max(nullif(pricing_unit, '')) as unit, - -- SavingsPlanNegation needs to be negated to prevent duplicate usage COST-5369 - sum( - CASE - WHEN lineitem_lineitemtype='SavingsPlanNegation' - THEN 0.0 - ELSE lineitem_usageamount - END - ) as usage_amount, - max(lineitem_normalizationfactor) as normalization_factor, - sum(lineitem_normalizedusageamount) as normalized_usage_amount, - max(lineitem_currencycode) as currency_code, - max(lineitem_unblendedrate) as unblended_rate, - /* SavingsPlanCoveredUsage entries have corresponding SavingsPlanNegation line items - that offset that cost. - https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html - */ - sum( - CASE - WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' - THEN 0.0 - ELSE lineitem_unblendedcost - END - ) as unblended_cost, - max(lineitem_blendedrate) as blended_rate, + max(date(lineitem_usagestartdate)) as usage_end, + max(lineitem_usageaccountid) as usage_account_id, + lineitem_resourceid as resource_id, + max(nullif(product_operatingsystem, '')) as operating_system, + max(nullif(product_region, '')) as region, + max(nullif(pricing_unit, '')) as unit, + -- SavingsPlanNegation needs to be negated to prevent duplicate usage COST-5369 + sum( + CASE + WHEN lineitem_lineitemtype='SavingsPlanNegation' + THEN 0.0 + ELSE lineitem_usageamount + END + ) as usage_amount, + max(lineitem_normalizationfactor) as normalization_factor, + sum(lineitem_normalizedusageamount) as normalized_usage_amount, + max(lineitem_currencycode) as currency_code, + max(lineitem_unblendedrate) as unblended_rate, + /* SavingsPlanCoveredUsage entries have corresponding SavingsPlanNegation line items + that offset that cost. + https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html + */ + sum( + CASE + WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' + THEN 0.0 + ELSE lineitem_unblendedcost + END + ) as unblended_cost, + max(lineitem_blendedrate) as blended_rate, /* SavingsPlanCoveredUsage entries have corresponding SavingsPlanNegation line items - that offset that cost. - https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html + that offset that cost. + https://docs.aws.amazon.com/cur/latest/userguide/cur-sp.html */ - sum( - CASE - WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' - THEN 0.0 - ELSE lineitem_blendedcost - END - ) as blended_cost, - sum(savingsplan_savingsplaneffectivecost) as savingsplan_effective_cost, - sum( - CASE - WHEN lineitem_lineitemtype='Tax' - OR lineitem_lineitemtype='Usage' - THEN lineitem_unblendedcost - ELSE savingsplan_savingsplaneffectivecost - END - ) as calculated_amortized_cost, - sum(pricing_publicondemandcost) as public_on_demand_cost, - max(pricing_publicondemandrate) as public_on_demand_rate + sum( + CASE + WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' + THEN 0.0 + ELSE lineitem_blendedcost + END + ) as blended_cost, + sum(savingsplan_savingsplaneffectivecost) as savingsplan_effective_cost, + sum( + CASE + WHEN lineitem_lineitemtype='SavingsPlanCoveredUsage' + OR lineitem_lineitemtype='SavingsPlanNegation' + THEN savingsplan_savingsplaneffectivecost + ELSE lineitem_unblendedcost + END + ) as calculated_amortized_cost, + sum(pricing_publicondemandcost) as public_on_demand_cost, + max(pricing_publicondemandrate) as public_on_demand_rate FROM hive.{{trino_schema_prefix | sqlsafe}}{{schema | sqlsafe}}.aws_line_items_daily as lid WHERE source = '{{source_uuid | sqlsafe}}' AND year = '{{year | sqlsafe}}'