Aggregate
BigQuery
APPROX_COUNT_DISTINCT
Estimates the number of distinct values using the HyperLogLog++ algorithm. Much faster and less resource-intensive than COUNT(DISTINCT) for large datasets.
Tipe hasil:
INT64Diperbarui: 13 Jun 2026Syntax
SQL
APPROX_COUNT_DISTINCT(expression)Parameter
expressionanywajib
A column or expression to estimate the number of distinct values for
Contoh Penggunaan
Estimate Unique Visitors
SQL
1 SELECT 2 APPROX_COUNT_DISTINCT(user_id) as approx_unique_users, 3 COUNT(DISTINCT user_id) as exact_unique_users 4 FROM `project.dataset.page_views` 5 WHERE DATE(timestamp) = '2024-06-25';
Compares approximate vs. exact count for unique users.
Hasil
| approx_unique_users | exact_unique_users |
|---|---|
| 1523847 | 1525000 |
Daily Unique Users with APPROX
SQL
1 SELECT 2 DATE(timestamp) as date, 3 APPROX_COUNT_DISTINCT(user_id) as daily_unique_users 4 FROM `project.dataset.events` 5 WHERE timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) 6 GROUP BY date 7 ORDER BY date DESC;
Efficiently calculates daily unique users.
Hasil
| date | daily_unique_users |
|---|---|
| 2024-06-25 | 125000 |
| 2024-06-24 | 118500 |
| 2024-06-23 | 132000 |
Multiple Approximate Counts
SQL
1 SELECT 2 country, 3 APPROX_COUNT_DISTINCT(user_id) as unique_users, 4 APPROX_COUNT_DISTINCT(session_id) as unique_sessions, 5 APPROX_COUNT_DISTINCT(device_id) as unique_devices 6 FROM `project.dataset.analytics` 7 GROUP BY country 8 ORDER BY unique_users DESC 9 LIMIT 10;
Multiple approximate counts per country.
Hasil
| country | unique_users | unique_sessions | unique_devices |
|---|---|---|---|
| Indonesia | 2500000 | 8750000 | 1850000 |
| Malaysia | 450000 | 1575000 | 380000 |
| Singapore | 320000 | 1120000 | 290000 |
Performance Comparison
SQL
1 -- This query can be 10-100x faster than COUNT(DISTINCT) 2 -- for tables with billions of rows 3 SELECT 4 APPROX_COUNT_DISTINCT(customer_id) as unique_customers, 5 APPROX_COUNT_DISTINCT(product_id) as unique_products, 6 APPROX_COUNT_DISTINCT(CONCAT(customer_id, '-', product_id)) as unique_pairs 7 FROM `project.dataset.transactions` 8 WHERE EXTRACT(YEAR FROM transaction_date) = 2024;
Multiple approximate counts on a large dataset.
Hasil
| unique_customers | unique_products | unique_pairs |
|---|---|---|
| 5200000 | 125000 | 45000000 |
Pertanyaan Umum tentang APPROX_COUNT_DISTINCT
Apa itu fungsi APPROX_COUNT_DISTINCT di BigQuery?
Estimates the number of distinct values using the HyperLogLog++ algorithm. Much faster and less resource-intensive than COUNT(DISTINCT) for large datasets. Di BigQuery, fungsi APPROX_COUNT_DISTINCT termasuk dalam kelompok fungsi yang sering digunakan untuk mengolah dan menganalisis data secara efisien.
Bagaimana cara menggunakan APPROX_COUNT_DISTINCT di BigQuery?
Gunakan sintaks berikut: APPROX_COUNT_DISTINCT(expression). Pastikan argumen yang dimasukkan sudah sesuai dengan tipe data yang diharapkan.
Apa nilai yang dikembalikan oleh fungsi APPROX_COUNT_DISTINCT?
Fungsi APPROX_COUNT_DISTINCT mengembalikan nilai bertipe INT64. Pastikan tipe data hasil sudah sesuai dengan kebutuhan query atau formula kamu. Jika input mengandung nilai NULL, perilaku fungsi dapat berbeda — selalu periksa dokumentasi untuk memastikan hasilnya sesuai ekspektasi.
Apa saja parameter fungsi APPROX_COUNT_DISTINCT?
Fungsi ini memiliki 1 parameter wajib. Parameter yang digunakan: expression (any, wajib): A column or expression to estimate the number of distinct values for.
Fungsi Terkait
Ngulik Langsung
Latihan Langsung →Sudah paham APPROX_COUNT_DISTINCT? Latih langsung di browser
Latihan interaktif, langsung di browser.