runtime
AI Platform Economics · milestone 01 / 04

milestone 1/4 Ratios, ROUND & NULLIF

Calculate safe ratios and round your results.

AI Platform Economics · Calculate success rates and usage costs, prevent join fan-out, and reconcile split payments with refunds.

approach

A rate is numerator divided by denominator. Use a real number (100.0) to avoid integer division. NULLIF(x,0) turns a zero denominator into NULL; ROUND controls displayed precision. Preserve that NULL instead of calling an undefined rate zero.

worked-example.sql
SELECT
  account_id,
  ROUND(100.0 * SUM(CASE WHEN status='success' THEN 1 ELSE 0 END)/NULLIF(COUNT(*),0),2) AS success_pct
FROM ai_requests
GROUP BY account_id;

data for this milestone

ai_requests7 columns · 12 rows
idinteger · keyaccount_idintegermodeltextrequested_attextinput_tokensintegeroutput_tokensintegerstatustext
11swift2025-01-01 10:00:001000200success
21reason2025-01-02 11:00:002000600success
32swift2025-01-02 12:00:005000error
43reason2025-01-02 13:00:0080002000success
53swift2025-01-03 14:00:0040001000success

showing 5 of 12 rows

ai_accounts4 columns · 6 rows
idinteger · keyplantextregiontextsigned_up_attext
1proUS2025-01-01
2freeUS2025-01-02
3teamEU2025-01-02
4proIN2025-01-03
5freeEU2025-01-04

showing 5 of 6 rows

challenge 1

Return account_id and success_pct (0–100, rounded to 2 decimals) for accounts with requests.

Return account_idsuccess_pctany row order

challenge-1.sql
 
3 steps left · check both queries and pass the concept checkNext milestone
Ratios, ROUND & NULLIF — AI Platform Economics · Runtime