-- Total transaction amount (streaming)SELECT sum(amount) as total_amountFROM transactions;-- Sum by group (historical)SELECT category, sum(amount) as category_totalFROM table(transactions)GROUP BY category;
-- Min and max values per device (streaming)SELECT device_id, min(temperature) as min_temp, max(temperature) as max_tempFROM sensor_readingsGROUP BY device_id;
-- Group by dateSELECT to_date(_tp_time) as event_date, count() as daily_countFROM table(events)GROUP BY event_date;-- Group by hourSELECT to_hour(_tp_time) as hour, count() as hourly_countFROM table(events)WHERE to_date(_tp_time) = today()GROUP BY hourORDER BY hour;
-- Get latest reading per deviceSELECT device_id, any_last(temperature) as latest_temp, max(_tp_time) as last_reading_timeFROM table(sensor_readings)WHERE _tp_time > now() - INTERVAL 5 MINUTEGROUP BY device_id;
-- Calculate percentilesSELECT device_id, quantile(0.5)(temperature) as median_temp, quantile(0.95)(temperature) as p95_temp, quantile(0.99)(temperature) as p99_tempFROM table(sensor_readings)GROUP BY device_id;
-- Only show users with more than 10 eventsSELECT user_id, count() as event_countFROM table(events)GROUP BY user_idHAVING count() > 10;-- Multiple conditionsSELECT category, sum(amount) as total, avg(amount) as averageFROM table(transactions)GROUP BY categoryHAVING sum(amount) > 1000 AND avg(amount) > 50;
-- Find users above average spendWITH user_totals AS ( SELECT user_id, sum(amount) as total_spend FROM table(transactions) GROUP BY user_id)SELECT user_id, total_spendFROM user_totalsWHERE total_spend > (SELECT avg(total_spend) FROM user_totals);
-- Average of daily totalsSELECT avg(daily_total) as avg_daily_totalFROM ( SELECT to_date(_tp_time) as date, sum(amount) as daily_total FROM table(transactions) GROUP BY date);
-- Streaming metrics with multiple aggregationsSELECT device_id, count() as event_count, avg(cpu_usage) as avg_cpu, max(cpu_usage) as max_cpu, avg(memory_usage) as avg_memory, max(memory_usage) as max_memoryFROM metricsGROUP BY device_idEMIT STREAM PERIODIC INTERVAL 10 SECOND;
-- Historical hourly aggregationSELECT to_start_of_hour(_tp_time) as hour, count() as transaction_count, sum(amount) as total_amount, avg(amount) as avg_amount, min(amount) as min_amount, max(amount) as max_amount, count(DISTINCT user_id) as unique_usersFROM table(transactions)WHERE _tp_time >= today() - INTERVAL 7 DAYGROUP BY hourORDER BY hour DESC;
-- Find top 10 users by transaction count and amountSELECT user_id, count() as tx_count, sum(amount) as total_amount, avg(amount) as avg_amountFROM table(transactions)WHERE _tp_time > now() - INTERVAL 30 DAYGROUP BY user_idORDER BY total_amount DESCLIMIT 10;
-- Response time percentiles per endpointSELECT endpoint, count() as request_count, quantile(0.50)(response_time) as p50_ms, quantile(0.90)(response_time) as p90_ms, quantile(0.95)(response_time) as p95_ms, quantile(0.99)(response_time) as p99_ms, max(response_time) as max_msFROM table(api_logs)WHERE _tp_time > now() - INTERVAL 1 HOURGROUP BY endpointORDER BY request_count DESC;
-- High-cardinality grouping requires more memorySELECT user_id, -- Could be millions of unique values product_id, -- Combined cardinality can be huge count() as cntFROM eventsGROUP BY user_id, product_id;
High-cardinality GROUP BY keys can consume significant memory in streaming queries. Monitor state size and consider using windows or filters to limit cardinality.
-- Use approximate functions for better performanceSELECT category, uniq(user_id) as approx_unique_users -- Faster than count(DISTINCT)FROM eventsGROUP BY category;