> ## Documentation Index
> Fetch the complete documentation index at: https://docs.jaantonio.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Relational joins

> Join Analytics views by ID without multiplying rows or totals.

Join detail views to their root with the documented IDs.

| Relationship     | Join key      |
| ---------------- | ------------- |
| Shipment details | `shipment_id` |
| Quote details    | `quote_id`    |
| Charge details   | `charge_id`   |
| Party details    | `party_id`    |

## Join a shipment to its charges

```sql theme={null}
SELECT s.business_key, c.charge_code, c.sell_local_amount
FROM reporting.shipments AS s
JOIN reporting.shipment_charges AS c
  ON c.shipment_id = s.shipment_id
ORDER BY s.business_key
LIMIT 100;
```

This result has one row per charge. Shipment values repeat when a shipment has several charges.

## Keep totals safe

Aggregate a detail before you join it to root totals.

<Warning>A direct one-to-many join repeats root values for every matching detail row.</Warning>

```sql theme={null}
WITH charges AS (
  SELECT shipment_id, SUM(sell_local_amount) AS charge_total
  FROM reporting.shipment_charges
  GROUP BY shipment_id
)
SELECT s.business_key, s.total_revenue, c.charge_total
FROM reporting.shipments AS s
LEFT JOIN charges AS c
  ON c.shipment_id = s.shipment_id;
```

Use `shipment_consols`, `quote_shipments`, and `quote_bookings` for relationships between business
objects. Do not create links from matching names, routes, dates, or amounts.
