# Example Queries
The queries below are examples of gaining insights using the data available on the Monta Data Warehouse. To reproduce these, replace `examplepartner`with the partnername we provide. 

### Count number of charges and KWH delivered on each Charging Station over months

```sql
with charges_agg as (
select
    date_trunc('month', completed_at) as month
  , charge_point_id
  , sum(watt_hours / 1000.0)          as total_monthly_kwh
  , count(distinct id)                as number_of_charges
  , max(completed_at)                 as latest_charge
from
    partner.production_examplepartner.charges
where charge_point_id in (select distinct id from partner.production_examplepartner.charge_points)
group by
    month, charge_point_id
)
select
    charges_agg.*
  , cps.* exclude (id)
from
    charges_agg
left join partner.production_examplepartner.charge_points cps
    on cps.id = charges_agg.charge_point_id;
```

### Calculate the amount of money each user has spent in a given account with a given vehicle, for each month.

Transaction amounts are returned in the transaction's `to_currency_code` — no EUR normalisation is applied as there is no EUR exchange rate column in the `transactions` table.

```sql
with totals as (
    select
        date_trunc('month', ch.completed_at) as billing_month
      , paying_team_id                        as team_id
      , ch.user_id
      , vehicle_id
      , sum(ch.watt_hours / 1000.0)           as consumption_kwh
      , sum(tr.to_sub_amount)                 as net_price
      , sum(tr.to_amount)                     as gross_price
      , sum(tr.vat_to_amount)                 as vat
      , count(distinct ch.id)                 as number_of_charges
    from
        partner.production_examplepartner.charges ch
    left join
        partner.production_examplepartner.transactions tr
        on  ch.id                     = tr.reference_id
        and tr.reference_type         = 'CHARGE'
        and ch.paying_team_id         = tr.from_wallet_owner_id
        and tr.from_wallet_owner_type = 'TEAM'
        and tr.state                  = 'complete'
    where
        ch.paying_team_id in (select distinct id from partner.production_examplepartner.teams)
        and ch.completed_at is not null
    group by
        billing_month
      , paying_team_id
      , ch.user_id
      , vehicle_id
)
select
    totals.*
  , users.first_name
  , users.last_name
  , users.email
from totals
left join partner.production_examplepartner.users users
    on users.id = totals.user_id
```

### List the locations on each of your Lists

`operator_list_items` points at its members with `entity_type` and `entity_id`. For a list of locations, `entity_id` is the location id.

```sql
select
    l.id   as list_id
  , l.name as list_name
  , s.*
from
    partner.production_examplepartner.operator_lists l
inner join partner.production_examplepartner.operator_list_items li
    on li.list_id = l.id
    and li.entity_type = 'site'
inner join partner.production_examplepartner.charge_point_sites s
    on s.id = li.entity_id
where l.deleted_at is null
order by l.name;
```
