Aggregate

COUNTIF

BigQueryBigQuery

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 2026

Syntax

SQL
COUNTIF(condition)

Parameter

conditionBOOLwajib

The boolean expression to evaluate for each row

Contoh Penggunaan

Count by Status

SQL
1SELECT
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
6FROM `project.dataset.orders`;

Breaks down orders by status in a single query.

Hasil
completed_orderspending_orderscancelled_orderstotal_orders
8500120030010000

COUNTIF with Multiple Conditions

SQL
1SELECT
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
6FROM `project.dataset.products`
7GROUP BY product_category;

Groups products by price range per category.

Hasil
product_categorypremium_productsmid_rangebudget_products
Electronics4512085
Fashion1289234

Conversion Rate Calculation

SQL
1SELECT
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
6FROM `project.dataset.user_sessions`
7GROUP BY date
8ORDER BY date DESC
9LIMIT 7;

Calculates the daily conversion rate.

Hasil
datetotal_visitsconversionsconversion_rate
2024-06-2512500037503.00
2024-06-2411800033042.80

COUNTIF with Date Conditions

SQL
1SELECT
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
6FROM `project.dataset.orders`
7GROUP BY customer_id
8HAVING COUNTIF(order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)) > 0
9LIMIT 10;

Analyzes customer activity by time period.

Hasil
customer_idorders_last_30_daysorders_last_90_daystotal_orders
C0013825
C00251245

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

Sudah paham COUNTIF? Latih langsung di browser

Latihan interaktif, langsung di browser.

Latihan Langsung →