-- Define a model from JSON file
model person = {
from 'person.json'
}
-- Query with filtering and aggregation
from person
where age >= 18 and status = 'active'
group by department
agg
employee_count = count(*),
avg_salary = avg(salary),
max_age = max(age)
order by employee_count desc
limit 10
Complex Query with Joins
---
Sales Analysis Query
@author Wvlet Team
---
-- Import required models
import 'models/sales.wv'
-- Define constants
val YEAR = 2024
val MIN_REVENUE = 1000.50
-- Main query with multiple joins
from orders
left join customers on orders.customer_id = customers.id
inner join products on orders.product_id = products.id
where
orders.order_date >= '2024-01-01' and
orders.status in ('completed', 'shipped')
select
order_id = orders.id,
customer_name = customers.name,
product_name = products.name,
quantity = orders.quantity,
unit_price = products.price,
discount_pct = orders.discount * 100,
total_amount = orders.quantity * products.price * (1 - orders.discount)
order by total_amount desc
Advanced Features
-- Window functions
from sales
select
product_id,
sale_date,
amount,
running_total = sum(amount) over (
partition by product_id
order by sale_date
rows between unbounded preceding and current row
),
rank = row_number() over (
partition by product_id
order by amount desc
)
-- Pivot operation
from monthly_sales
pivot month
agg
revenue = sum(amount),
transactions = count(*)
-- Test assertions
test _.size should be 100
test _.columns should contain ['user_id', 'name', 'email']
test _.output should be """
| id | status |
|----|---------|
| 1 | active |
| 2 | pending |
"""
-- Save results
save to 'output/results.parquet'