Aggregate
BigQuery
COUNTIF
Counts the number of rows that satisfy a given condition. Provides a more concise and readable syntax compared to COUNT with CASE or IF.
Tipe hasil:
INT64Diperbarui: 13 Jun 2026Syntax
SQL
COUNTIF(condition)Parameter
conditionBOOLwajib
The boolean expression to evaluate for each row
Contoh Penggunaan
Count by Status
SQL
1 SELECT 2 COUNTIF(status = 'completed') as completed_orders, 3 COUNTIF(status = 'pending') as pending_orders, 4 COUNTIF(status = 'cancelled') as cancelled_orders, 5 COUNT(*) as total_orders 6 FROM `project.dataset.orders`;
Breaks down orders by status in a single query.
Hasil
| completed_orders | pending_orders | cancelled_orders | total_orders |
|---|---|---|---|
| 8500 | 1200 | 300 | 10000 |
COUNTIF with Multiple Conditions
SQL
1 SELECT 2 product_category, 3 COUNTIF(price > 1000000) as premium_products, 4 COUNTIF(price BETWEEN 500000 AND 1000000) as mid_range, 5 COUNTIF(price < 500000) as budget_products 6 FROM `project.dataset.products` 7 GROUP BY product_category;
Groups products by price range per category.
Hasil
| product_category | premium_products | mid_range | budget_products |
|---|---|---|---|
| Electronics | 45 | 120 | 85 |
| Fashion | 12 | 89 | 234 |
Conversion Rate Calculation
SQL
1 SELECT 2 DATE(timestamp) as date, 3 COUNT(*) as total_visits, 4 COUNTIF(converted = TRUE) as conversions, 5 ROUND(COUNTIF(converted = TRUE) * 100.0 / COUNT(*), 2) as conversion_rate 6 FROM `project.dataset.user_sessions` 7 GROUP BY date 8 ORDER BY date DESC 9 LIMIT 7;
Calculates the daily conversion rate.
Hasil
| date | total_visits | conversions | conversion_rate |
|---|---|---|---|
| 2024-06-25 | 125000 | 3750 | 3.00 |
| 2024-06-24 | 118000 | 3304 | 2.80 |
COUNTIF with Date Conditions
SQL
1 SELECT 2 customer_id, 3 COUNTIF(order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)) as orders_last_30_days, 4 COUNTIF(order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY)) as orders_last_90_days, 5 COUNT(*) as total_orders 6 FROM `project.dataset.orders` 7 GROUP BY customer_id 8 HAVING COUNTIF(order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)) > 0 9 LIMIT 10;
Analyzes customer activity by time period.
Hasil
| customer_id | orders_last_30_days | orders_last_90_days | total_orders |
|---|---|---|---|
| C001 | 3 | 8 | 25 |
| C002 | 5 | 12 | 45 |
Pertanyaan Umum tentang COUNTIF
Apa itu fungsi COUNTIF di BigQuery?
Counts the number of rows that satisfy a given condition. Provides a more concise and readable syntax compared to COUNT with CASE or IF. Di BigQuery, fungsi COUNTIF termasuk dalam kelompok fungsi yang sering digunakan untuk mengolah dan menganalisis data secara efisien.
Bagaimana cara menggunakan COUNTIF di BigQuery?
Gunakan sintaks berikut: COUNTIF(condition). Pastikan argumen yang dimasukkan sudah sesuai dengan tipe data yang diharapkan.
Apa nilai yang dikembalikan oleh fungsi COUNTIF?
Fungsi COUNTIF 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 COUNTIF?
Fungsi ini memiliki 1 parameter wajib. Parameter yang digunakan: condition (BOOL, wajib): The boolean expression to evaluate for each row.
Ngulik Langsung
Latihan Langsung →Sudah paham COUNTIF? Latih langsung di browser
Latihan interaktif, langsung di browser.