Aggregate
Agregasi data dengan SUM, COUNT, AVG, MIN, MAX dan GROUP BY
Endpoint /aggregate menjalankan fungsi agregasi pada data endpoint tanpa perlu menulis query SQL secara manual. Mendukung lima fungsi standar (COUNT, SUM, AVG, MIN, MAX), pengelompokan data dengan GROUP BY, filter hasil dengan HAVING, dan JOIN ke tabel lain.
Kapan Menggunakan /aggregate (When to Use /aggregate)
| Skenario | Contoh |
|---|---|
| Menghitung jumlah record | Total supplier aktif |
| Menjumlahkan nilai numerik | Total stok seluruh produk |
| Menghitung rata-rata | Rata-rata harga jual produk per kategori |
| Mencari nilai minimum/maksimum | Harga terendah dan tertinggi per kategori |
| Laporan pengelompokan | Jumlah produk dan total stok per kategori |
Konfigurasi Payload (Payload Configuration)
Aktifkan endpoint aggregate di payload:
{
"tableName": "item_product",
"primaryKey": "item_product_id",
"action": {
"aggregate": true
}
}Fungsi Agregasi yang Tersedia (Available Aggregation Functions)
| Fungsi | Keterangan | Field |
|---|---|---|
count | Menghitung jumlah record | "*" (wildcard) atau nama kolom |
sum | Menjumlahkan nilai | Kolom numerik |
avg | Menghitung rata-rata | Kolom numerik |
min | Nilai minimum | Kolom numerik atau tanggal |
max | Nilai maksimum | Kolom numerik atau tanggal |
Wildcard "*" hanya valid untuk fungsi count. Fungsi lain memerlukan nama kolom spesifik.
Contoh Penggunaan (Usage Examples)
Count Sederhana (Simple Count)
Menghitung total supplier:
{
"operations": [
{ "function": "count", "field": "*", "alias": "total_supplier" }
]
}Response:
{
"success": true,
"data": {
"total_supplier": 25
},
"timestamp": "2026-04-16T10:30:00.000Z"
}Beberapa Fungsi Sekaligus (Multiple Functions)
Menghitung total produk, total stok, dan rata-rata harga:
{
"operations": [
{ "function": "count", "field": "*", "alias": "total_items" },
{ "function": "sum", "field": "stock", "alias": "total_stock" },
{ "function": "avg", "field": "selling_price", "alias": "avg_price" }
]
}Response:
{
"success": true,
"data": {
"total_items": 150,
"total_stock": 4500,
"avg_price": 125000
},
"timestamp": "2026-04-16T10:30:00.000Z"
}GROUP BY — Pengelompokan Data (Data Grouping)
Menghitung jumlah produk dan total stok per kategori:
{
"operations": [
{ "function": "count", "field": "*", "alias": "item_count" },
{ "function": "sum", "field": "stock", "alias": "total_stock" }
],
"group_by": ["category_id"],
"sort_columns": [
{ "column": "total_stock", "direction": "DESC" }
]
}Response berisi array data yang dikelompokkan:
{
"success": true,
"data": [
{ "category_id": "cat-001", "item_count": 15, "total_stock": 450 },
{ "category_id": "cat-002", "item_count": 8, "total_stock": 120 },
{ "category_id": "cat-003", "item_count": 5, "total_stock": 75 }
],
"timestamp": "2026-04-16T10:30:00.000Z"
}GROUP BY dengan HAVING
Filter hasil agregasi: hanya kategori dengan total stok 100 atau lebih:
{
"operations": [
{ "function": "count", "field": "*", "alias": "item_count" },
{ "function": "sum", "field": "stock", "alias": "total_stock" }
],
"group_by": ["category_id"],
"having": {
"conditions": [
{ "key": "total_stock", "operator": ">=", "value": 100 }
]
}
}HAVING hanya dapat digunakan bersamaan dengan GROUP BY. Request yang mengirim HAVING tanpa GROUP BY akan ditolak dengan error 400.
Agregasi dengan JOIN
Untuk menampilkan nama kategori (bukan hanya ID) dalam hasil GROUP BY, gunakan parameter joins:
{
"operations": [
{ "function": "count", "field": "*", "alias": "item_count" },
{ "function": "sum", "field": "stock", "alias": "total_stock" }
],
"joins": [
{
"tableName": "category",
"joinType": "LEFT",
"sourceField": "category_id",
"targetField": "category_id",
"fields": ["category_name"]
}
],
"group_by": ["category_name"],
"sort_columns": [
{ "column": "total_stock", "direction": "DESC" }
]
}Response:
{
"success": true,
"data": [
{ "category_name": "Electronics", "item_count": 15, "total_stock": 450 },
{ "category_name": "Furniture", "item_count": 8, "total_stock": 120 }
],
"timestamp": "2026-04-16T10:30:00.000Z"
}Agregasi dengan Filter WHERE
Menghitung total stok hanya untuk produk aktif:
{
"operations": [
{ "function": "sum", "field": "stock", "alias": "active_stock" }
],
"where": [
{ "key": "is_active", "value": true }
]
}Konfigurasi JOIN (JOIN Configuration)
| Property | Tipe | Wajib | Keterangan |
|---|---|---|---|
tableName | string | Ya | Nama tabel yang akan di-JOIN |
joinType | string | Tidak | "LEFT", "INNER", atau "RIGHT" (default: "LEFT") |
sourceField | string | Ya | Kolom foreign key di tabel utama |
targetField | string | Ya | Kolom primary key di tabel target |
fields | array | Ya | Kolom dari tabel target yang diambil |
Format Response (Response Format)
| Kondisi | Format Data |
|---|---|
| Tanpa GROUP BY | data berisi satu object { alias1: value1, alias2: value2 } |
| Dengan GROUP BY | data berisi array [ { group_col: val, alias1: val1 }, ... ] |
Langkah Selanjutnya (Next Steps)
- Read — List Data untuk pengambilan data dengan filter dan pagination
- DataTables untuk tabel data interaktif di browser
- POST /aggregate untuk spesifikasi teknis lengkap