Sub topic 15
From queries to dashboards and alerts
This chapter assumes you can write and run queries in the SQL editor, use three-level Unity Catalog names, and save a query. It shows how to turn saved queries into dashboards and alerts, and what each one costs in warehouse time.
Where dashboards and alerts fit
A query returns a result once. Dashboards and alerts make that result useful over time.
- A dashboard is a page of visualizations and filters built from queries. People open it to see the current numbers.
- An alert runs a query on a schedule and checks one value in the result against a condition. If the condition is met, it sends a notification.
Both run their queries on a SQL warehouse. A viewer opening a dashboard or a schedule firing an alert uses warehouse time, so the warehouse you pick affects cost and speed.
Building a dashboard
Start with a query that gives a result worth charting. This one uses the sample taxi data:
SELECT
pickup_zip,
COUNT(*) AS trips,
ROUND(AVG(fare_amount), 2) AS avg_fare
FROM samples.nyctaxi.trips
GROUP BY pickup_zip
ORDER BY trips DESC
LIMIT 20;To build a dashboard from it:
- Create a new dashboard and add a dataset. A dataset is a query that supplies data to the dashboard. Paste the query above.
- Add a visualization, such as a bar chart with
pickup_zipon the x axis andtripson the y axis. - Add a filter widget if you want viewers to narrow the data. A parameter in the dataset query, like
:min_distance, can be linked to a widget. - Choose the warehouse that runs the dashboard's queries, then publish.
Publishing makes a read-only version for viewers. You can edit the draft without changing what viewers see.
Setting up an alert
An alert watches a single value. Write a query that returns the value, then set a condition on it.
SELECT COUNT(*) AS trips_last_day
FROM samples.nyctaxi.trips
WHERE tpep_pickup_datetime >= current_timestamp() - INTERVAL 1 DAY;In the alert settings you choose:
- The query and the column to check, here
trips_last_day. - The condition, for example "value is less than 100".
- The schedule, for example every hour.
- The recipients, such as email addresses or a notification destination.
When the condition becomes true, the alert triggers and notifies the recipients.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Dashboard shows a permission error | The viewer cannot use the warehouse or read the tables | Grant warehouse access and table privileges, or publish with the owner's credentials |
| Dashboard loads slowly after idle time | The warehouse was auto stopped and must start | Use a serverless warehouse, or accept the start delay |
| Alert never triggers | The query returns a different row or column than the condition checks | Run the query manually and confirm the first row of the chosen column |
| Warehouse bill grows | A short refresh schedule keeps the warehouse awake | Lengthen the schedule so auto stop can take effect |
Exercise
An alert query runs every 5 minutes on a warehouse with a 10-minute auto stop. What happens to the warehouse, and what is one way to reduce cost?
Show answer
The warehouse never reaches 10 idle minutes, so it stays running continuously. To reduce cost, run the alert less often than the auto stop time, such as every hour, or use a serverless warehouse, which starts quickly and can use a short auto stop.