Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
GM model optimizations (singer-io#69)
Browse files Browse the repository at this point in the history
* WIP optimizations

* Remove commented code
  • Loading branch information
jacobfrackson authored Nov 23, 2018
1 parent 7778a73 commit 9dda871
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
38 changes: 13 additions & 25 deletions models/meta/medops/medops_gm_by_contract_daily.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
with user_contract as (
select * from {{ ref ( 'scribe_user_contract_detailed' ) }}
)

, contracts as (
select * from {{ ref ( 'scribe_contracts_detailed' ) }}
)

, organizations as (
select * from {{ ref ( 'organizations' ) }}
select * from {{ ref ( 'user_contract' ) }}
)

, daily_costs as (
Expand All @@ -25,29 +17,25 @@ with user_contract as (

, daily_revenue as (
select days.date_day
, contracts.contract_id
, contracts.organization_name
, organizations.account_name
, coalesce(user_contract.residence_province, organizations.province)
as residence_province
, contracts.charge_strategy
, case when contracts.charge_strategy = 'dynamic'
then contracts.charge_price
, user_contract.contract_id
, user_contract.organization_name
, user_contract.account_name
, user_contract.residence_province
, user_contract.charge_strategy
, case when user_contract.charge_strategy = 'dynamic'
then user_contract.charge_price
/ days.days_in_month
when contracts.charge_strategy = 'auto_dynamic'
when user_contract.charge_strategy = 'auto_dynamic'
then 15 / days.days_in_month
when contracts.organization_id = '230'
when user_contract.organization_id = '230'
then 1 / days.days_in_month
when contracts.charge_strategy = 'fixed'
when user_contract.charge_strategy = 'fixed'
then 9 / days.days_in_month
else 0
end as charge_price
from days
inner join contracts
on contracts.during @> days.date_day
left join organizations
using (organization_id)
inner join user_contract using (contract_id)
inner join user_contract
on days.date_day <@ user_contract.during
where user_contract.is_employee
)

Expand Down
10 changes: 9 additions & 1 deletion models/scribe/table/scribe_user_contract_detailed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ with
, user_contract.user_id
, contracts.organization_id
, contracts.participant_id
, contracts.charge_price
, contracts.charge_strategy

-- Organize during fields for filtering out overlapping contracts
, contracts.during_start
Expand All @@ -37,7 +39,11 @@ with
, organizations.organization_name
, organizations.is_paid as organization_is_paid
, user_contract.user_id = contracts.participant_id as is_employee
, coalesce(users.residence_province, contracts.admin_area_name) as residence_province
, coalesce(
users.residence_province,
organizations.province,
contracts.admin_area_name
) as residence_province
, users.country
, users.birthday
, users.gender
Expand Down Expand Up @@ -82,6 +88,8 @@ select contract_id
, user_id
, organization_id
, participant_id
, charge_price
, charge_strategy
-- correct during_start to eliminate overlap
-- TODO: add a data test to test all known use cases
, case when previous_during_start is null then during_start
Expand Down

0 comments on commit 9dda871

Please sign in to comment.