Rank Equipment Maintenance
Beginner Mode

Scenario

A pharmaceutical company tracks its lab equipment and associated maintenance records in two separate tables.

Task

Write a Snowflake SQL query that:

  1. Joins {{ ref("equipment") }} with {{ ref("maintenance_logs") }} to include only equipment that has maintenance records
  2. Aggregates each piece of equipment to find the most recent maintenance date and the sum of all maintenance costs
  3. Applies a dense rank based on total maintenance cost in descending order (rank 1 = highest cost)
  4. Returns only equipment ranked in the top 3 using QUALIFY
  5. Returns the columns: equipment_id, equipment_name, purchase_date, latest_maintenance_date, maintenance_cost_rank (cast as INTEGER)

Schema

equipment

Column Type Description
equipment_id String Unique identifier for each piece of equipment
equipment_name String Name of the equipment
purchase_date Date Date when the equipment was purchased

maintenance_logs

Column Type Description
equipment_id String Foreign key referencing equipment
maintenance_date Date Date of the maintenance event
maintenance_cost Float Cost of the maintenance event

Example

equipment:

equipment_id equipment_name purchase_date
E101 Centrifuge 2021-03-15
E102 Spectrometer 2021-06-20
E103 Incubator 2021-09-10
E104 Autoclave 2022-01-05
E105 Chromatograph 2022-04-18

maintenance_logs:

equipment_id maintenance_date maintenance_cost
E101 2023-01-10 1200.00
E101 2023-07-22 850.00
E102 2023-03-05 2100.00
E103 2023-05-14 950.00
E103 2023-11-30 1050.00
E104 2023-08-19 600.00

Expected Output:

equipment_id equipment_name purchase_date latest_maintenance_date maintenance_cost_rank
E102 Spectrometer 2021-06-20 2023-03-05 1
E101 Centrifuge 2021-03-15 2023-07-22 2
E103 Incubator 2021-09-10 2023-11-30 3

Note: E102 has the highest total maintenance cost (2100), followed by E101 (2050) and E103 (2000). E104 is excluded because its rank (4) falls outside the top 3. E105 has no maintenance records and is therefore excluded.

Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →