view: KNR_Ownership_Run_Prod_PostApproval_Summary {
    derived_table: {
      sql:

          select
          ari.accounting_period_id,
          rcc.run_controller_id,
          rc.run_controller_name,
          arr.accounting_run_id,
          arr.accounting_run_name,
          cts.account_id,
          a.account_name,
          cts.contract_id,
          c.contract_name,
      -- Payee Currency
          apt.currency_code,
          sum(nvl(arr.gross_revenue_payee_currency,0)) as gross_revenue_payee_currency,
          sum(nvl(arr.withholding_tax_payee_currency,0)) as withholding_tax_payee_currency,
          sum(nvl(arr.gross_revenue_after_withholding_tax_payee_currency,0)) as gross_revenue_after_withholding_tax_payee_currency,
          sum(nvl(arr.net_revenue_payee_currency,0)) as net_revenue_payee_currency
      -- sales
      from royalty_accounting.prod.accounting_run_results_distro as arr
      join royalty_accounting.prod.stmt_db_sales_distro as txn on arr.txn_id = txn.stmt_db_sales_distro_txn_id
      -- contract
      join royalty_accounting.prod.contract_transaction_staging as cts on arr.txn_id = cts.txn_id and cts.accounting_run_id=arr.accounting_run_id
      join orchard_app_reporting_v2.prod_royalty_accounting_royalty_accounting.contract as c on cts.contract_id = c.contract_id
      join orchard_app_reporting_v2.prod_royalty_accounting_royalty_accounting.run_controller_contract rcc on rcc.contract_id=c.contract_id
      join orchard_app_reporting_v2.prod_royalty_accounting_royalty_accounting.run_controller rc on rc.run_controller_id = rcc.run_controller_id
      -- account
      join orchard_app_reporting_v2.prod_royalty_accounting_royalty_accounting.account as a on cts.account_id = a.account_id
      join orchard_app_reporting_v2.prod_royalty_accounting_royalty_accounting.account_payment_term as apt on a.account_id = apt.account_id
    --account period
join orchard_app_reporting_v2.prod_royalty_accounting_royalty_accounting.accounting_run as ari on arr.accounting_run_id = ari.accounting_run_id

     -- predicates
      where true
      group by 1,2,3,4,5,6,7,8,9,10
      ;;
    }

  dimension: Accounting_Period_ID {
    type:  string
    sql: ${TABLE}.accounting_period_id ;;
  }

    dimension: Run_Controller_ID {
      type:  string
      sql: ${TABLE}.run_controller_id ;;
    }

    dimension: Run_Controller_Name {
      type:  string
      sql: ${TABLE}.run_controller_name ;;
    }
    dimension: Accounting_Run_ID {
      type:  string
      sql: ${TABLE}.accounting_run_id ;;
    }

    dimension: Accounting_Run_Name {
      type:  string
      sql: ${TABLE}.accounting_run_name ;;
    }

    dimension: Account_ID {
      type:  string
      sql: ${TABLE}.account_id ;;
    }

    dimension: Account_Name {
      type: string
      sql: ${TABLE}.account_name ;;
    }

    dimension: Contract_ID {
      type: string
      sql: ${TABLE}.contract_ID ;;
    }

    dimension: Contract_Name {
      type:  string
      sql: ${TABLE}.contract_Name ;;
    }

    dimension: Payee_Currency {
      type: string
      sql: ${TABLE}.currency_code ;;
    }


    measure: Gross_Revenue_Payee_Currency {
      type: sum
      sql: ${TABLE}.GROSS_REVENUE_PAYEE_CURRENCY ;;
      value_format: "#,##0.00"
    }

    measure: Withholding_Tax_Payee_Currency {
      type: sum
      sql: ${TABLE}.withholding_tax_payee_currency ;;
      value_format: "#,##0.00"
    }

    measure: Gross_Revenue_After_Withholding_Tax_Payee_Currency {
      type: sum
      sql: ${TABLE}.gross_revenue_after_withholding_tax_payee_currency ;;
      value_format: "#,##0.00"
    }

    measure: Net_Revenue_Payee_Currency {
      type: sum
      sql: ${TABLE}.net_revenue_payee_currency ;;
      value_format: "#,##0.00"
    }
  }
