Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

SQL (Structure Query Language)

Presentasi serupa


Presentasi berjudul: "SQL (Structure Query Language)"— Transcript presentasi:

1 SQL (Structure Query Language)
SQL (Structure Query Language) Materi 9 – Cross Tab (Pivot Table)

2 Definisi Pivot Table Crosstab Today! : Tujuan Pembelajaran Cross tab

3 Definisi Pivot Table Pengertian Kegunaan Pivot Table

4 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.

5 CROSSTAB

6 Tabel 1

7 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;

8 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;

9 Customer Table

10 Orders Table

11 TYPICAL (REGULAR) QUERY TO DISPLAY ORDER DATA
SELECT * FROM ORDERS;

12 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;

13 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

14 Bagaimana dengan ini ? Customer_name Frek_Beli_Januari
Frek_Beli_Februari Frek_Beli_Maret Irwan 1 Doddy Agus

15 Customer_name Frek_2005 Frek_Beli_Februari Frek_Beli_Maret Irwan 1 Doddy Agus

16 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;

17 Next : Pengumpulan Tugas Using Sub Query


Download ppt "SQL (Structure Query Language)"

Presentasi serupa


Iklan oleh Google