SQL (Structure Query Language) syurfah@gmail.com SQL (Structure Query Language) Materi 9 – Cross Tab (Pivot Table)
Definisi Pivot Table Crosstab Today! : Tujuan Pembelajaran Cross tab
Definisi Pivot Table Pengertian Kegunaan Pivot Table
PENGERTIAN Pivot table adalah sebuah tabel interaktif yg mampu menampilkan ringkasan data dalam jumlah yg besar, dan tanpa merubah tabel asli. Pivot table memiliki banyak cara untuk mengolah data, dengan cara menyaring data tertentu, memutar susunan data dalam kolom menjadi baris per baris, dan meringkas sudut pandang yang dikehendaki. Dalam melakukan perhitungan, PivotTable bukan hanya melakukan penjumlahan saja, melainkan penghitungan dengan fungsi yang lain seperti mencari rata-rata, mencari prosentase, standard deviasi, dan lainnya.
CROSSTAB
Tabel 1
select nopel , sum(case when kategori='INQUIRY' then 1 else 0 end) inquiry , sum(case when kategori='REQUEST' then 1 else 0 end) request , sum(case when kategori='COMPLAINT' then 1 else 0 end) complaint , sum(case when kategori='INQUIRY' then 1 else 0 end) + sum(case when kategori='REQUEST' then 1 else 0 end) + sum(case when kategori='COMPLAINT' then 1 else 0 end) total from tbl_data group by nopel;
select nopel , sum(case when kategori='INQUIRY' then 1 else 0 end) inquiry , sum(case when kategori='REQUEST' then 1 else 0 end) request , sum(case when kategori='COMPLAINT' then 1 else 0 end) complaint , sum(case when kategori='INQUIRY' then 1 else 0 end) + sum(case when kategori='REQUEST' then 1 else 0 end) + sum(case when kategori='COMPLAINT' then 1 else 0 end) total from tbl_data group by nopel;
Customer Table
Orders Table
TYPICAL (REGULAR) QUERY TO DISPLAY ORDER DATA SELECT * FROM ORDERS;
Database Northwind Bagaimana menampilkan fungsi aggregate pesanan utk pelanggan kita? Menghitung total biaya pengiriman untuk setiap pelanggan, dengan perusahaan pelayaran pecah sebagai kolom. select CustomerID as "Total Freight by Customer" , sum(CASE WHEN ShipVia = 1 then Freight ELSE 0 END) as "Speedy Express" , sum(CASE WHEN ShipVia = 2 then Freight ELSE 0 END) as "United Package" , sum(CASE WHEN ShipVia = 3 then Freight ELSE 0 END) as "Federal Shipping" from Orders group by CustomerID order by CustomerID;
select CustomerID as [Average Freight by Customer] , avg(CASE WHEN ShipVia = 1 then Freight ELSE null END) as [Speedy Express] , avg(CASE WHEN ShipVia = 2 then Freight ELSE null END) as [United Package] , avg(CASE WHEN ShipVia = 3 then Freight ELSE null END) as [Federal Shipping] from Orders group by CustomerID order by CustomerID select CustomerID as [Orders Shipped by Customer] , sum(CASE WHEN ShipVia = 1 then 1 ELSE 0 END) as [Speedy Express] , sum(CASE WHEN ShipVia = 2 then 1 ELSE 0 END) as [United Package] , sum(CASE WHEN ShipVia = 3 then 1 ELSE 0 END) as [Federal Shipping] from Orders group by CustomerID order by CustomerID
Bagaimana dengan ini ? Customer_name Frek_Beli_Januari Frek_Beli_Februari Frek_Beli_Maret Irwan 1 Doddy Agus
Customer_name Frek_2005 Frek_Beli_Februari Frek_Beli_Maret Irwan 1 Doddy Agus
Crosstab Query select job_id, department_id, count(*) from employees group by job_id, department_id; select department_id, job_id, sum(salary) sal from employees group by department_id, job_id; select job_id, max( decode( department_id, 10, cnt, null ) ) dept_10, max( decode( department_id, 20, cnt, null ) ) dept_20, max( decode( department_id, 30, cnt, null ) ) dept_30, max( decode( department_id, 40, cnt, null ) ) dept_40 from ( select job_id, department_id, count(*) cnt from employees group by job_id, department_id ) group by job_id; select department_id, sum( decode( job_id, 'CLERK', salary ) ) clerk, sum( decode( job, 'SALESMAN', salary ) ) salesman, sum( decode( job, 'MANAGER', salary ) ) manager, sum( decode( job, 'ANALYST', salary ) ) analyst, sum( decode( job, 'PRESIDENT', salary ) ) president group by department_id order by department_id;
Next : Pengumpulan Tugas Using Sub Query