# Price Groups
Contains pricing group configurations and their application metrics across charge points,
sites, and team members. Includes both team-owned and operator-owned price groups,
providing comprehensive counts and aggregations for both total and non-deleted entities
to support pricing strategy analysis and billing operations. Scopes data to the
partner's teams and operator.

:::info{title="New model"}

This table is the replacement for the soon-to-be-deprecated [`team_price_groups`](/docs/team_price_groups). It includes both team-owned and operator-owned price groups.

:::

**SQL:** `price_groups`

## Schema

| Column Name                          | Data Type     | Description                                                                                                             |
|:-------------------------------------|:--------------|:------------------------------------------------------------------------------------------------------------------------|
| **Primary ID**                       |               |                                                                                                                         |
| id                                   | INTEGER       | Primary key                                                                                                             |
| **Other IDs**                        |               |                                                                                                                         |
| owner_id                             | INTEGER       | ID of the owner (team or operator) that owns this price group                                                            |
| owner_type                           | VARCHAR       | Type of owner — `Team` for team-owned price groups, `Operator` for operator-owned                                        |
| team_id                              | INTEGER       | Foreign key reference to the team associated with this price group (null for operator-owned price groups)               |
| operator_id                          | INTEGER       | Foreign key reference to the operator managing this price group                                                         |
| external_id                          | VARCHAR       | External system identifier for the price group used for integration purposes                                            |
| **Timestamps**                       |               |                                                                                                                         |
| created_at                           | TIMESTAMP_NTZ | Timestamp when the price group record was originally created                                                            |
| updated_at                           | TIMESTAMP_NTZ | Timestamp when the price group record was last modified                                                                 |
| deleted_at                           | TIMESTAMP_NTZ | Timestamp when the price group was soft deleted, null if still active                                                   |
| **Filters**                          |               |                                                                                                                         |
| is_main_operator_match               | BOOLEAN       | Indicates whether this price group belongs to the main operator (true) or a different operator (false)                  |
| **Attributes**                       |               |                                                                                                                         |
| type                                 | VARCHAR       | Classification or category of the price group configuration                                                             |
| name                                 | VARCHAR       | Human-readable name assigned to the price group for identification                                                      |
| team_member_count                    | INTEGER       | Number of team members currently associated with this price group                                                       |
| charge_point_count                   | INTEGER       | Number of charge points currently associated with this price group                                                      |
| **Pricing Flags**                    |               |                                                                                                                         |
| pricings_count                       | INTEGER       | Total number of pricing configurations associated with this price group                                                  |
| has_pricings                         | BOOLEAN       | Indicates whether this price group has any pricing configurations defined (true) or not (false)                          |
| **Applied To Aggregations**          |               |                                                                                                                         |
| applied_to_charge_points_total       | INTEGER       | Total count of charge points where this price group is applied, including deleted entities                              |
| applied_to_sites_total               | INTEGER       | Total count of sites where this price group is applied, including deleted entities                                      |
| applied_to_team_members_total        | INTEGER       | Total count of team members where this price group is applied, including deleted entities                               |
| applied_to_charge_points_non_deleted | INTEGER       | Count of active charge points where this price group is applied, excluding soft-deleted entities                        |
| applied_to_sites_non_deleted         | INTEGER       | Count of active sites where this price group is applied, excluding soft-deleted entities                                |
| applied_to_team_members_non_deleted  | INTEGER       | Count of active team members where this price group is applied, excluding soft-deleted entities                         |
| **Count Aggregates**                 |               |                                                                                                                         |
| charge_point_count_total             | INTEGER       | Aggregate count of all charge points associated with this price group, including deleted entities                       |
| site_count_total                     | INTEGER       | Aggregate count of all sites associated with this price group, including deleted entities                               |
| team_member_count_total              | INTEGER       | Aggregate count of all team members associated with this price group, including deleted entities                        |
| charge_point_count_non_deleted       | INTEGER       | Aggregate count of active charge points associated with this price group, excluding soft-deleted entities               |
| site_count_non_deleted               | INTEGER       | Aggregate count of active sites associated with this price group, excluding soft-deleted entities                       |
| team_member_count_non_deleted        | INTEGER       | Aggregate count of active team members associated with this price group, excluding soft-deleted entities                |
| legacy_count_valid_rows              | INTEGER       | Number of valid data rows used in calculations for this price group record (used to match with legacy version of model) |

## Inclusion Criteria
* **Team-owned price groups:** the `team_id` must exist in the partner's `teams` table
* **Operator-owned price groups:** the `operator_id` must match the partner's operator ID or a sub-operator's operator ID

## Data flow
| Upstream tables | Downstream tables             |
|:----------------|:------------------------------|
| `teams` | `price_group_pricings` |
| `charge_points` | `charge_member_costs` |

## Alignment
### Monta API
- Aligns with Monta API [get/price-groups](https://developer.monta.com/api/v1/price-groups/get) endpoint
- The API now supports price groups at both the team and operator level
- The API excludes rows where `deleted_at is not null`

## Migration from `team_price_groups`
This table replaces `team_price_groups` and includes all price groups (both team-owned and operator-owned). The `team_price_groups` table remains available during a transition period. Key differences:
- Adds `owner_id` and `owner_type` columns to distinguish team-owned vs operator-owned price groups
- `team_id` is null for operator-owned price groups
- `operator_id` is derived from the team for team-owned price groups, and from `owner_id` for operator-owned price groups

## Changes vs previous version

### Summary
- Adds `owner_id`, `owner_type`, `pricings_count`, and `has_pricings` columns
- Includes operator-owned price groups as new rows — filter on `owner_type = 'Team'` to get the same results as `team_price_groups`

### Added columns (4)
- `owner_id` — ID of the owner (team or operator) that owns this price group
- `owner_type` — Type of owner (`Team` or `Operator`)
- `pricings_count` — Total number of pricing configurations associated with this price group
- `has_pricings` — Whether this price group has any pricing configurations defined

### Removed columns (0)

### Renamed columns (0)

### New rows
Operator-owned price groups are now included. To replicate the old `team_price_groups` results, filter on `owner_type = 'Team'`.
