Wednesday, April 7, 2021

.::: Sample Aljabar(Algebra): union(gabungan), intersection(irisan), MINUS (difference), distinct (proyeksi) In SQL :::.


1. UNION (Combine/gabungan)

union bertujuan menggabungkan dua query atau lebih menjadi satu kesatuan query dengan syarat query/subquery yang akan digabung harus memiliki domain kolom yang sama
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SELECT         field 1,...,field n,[agregate function]
FROM         table
 [WHERE]     [criteria]
 [GROUP BY] [field 1,...,field n]
UNION
SELECT         field 1,...,field n,[agregate function]
FROM         table
 [WHERE]     [kriteria]
 [GROUP BY] [field 1,...,field n]

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

select KODE_SUPLIER,count(JUMLAH_PASOK) as CACAH_PASOK, sum(JUMLAH_PASOK) as TOTAL_PASOK from pasok where KODE_SUPLIER='EJ-01' group by KODE_SUPLIER;
select KODE_SUPLIER,count(JUMLAH_PASOK) as CACAH_PASOK, sum(JUMLAH_PASOK) as TOTAL_PASOK from pasok where KODE_SUPLIER='EJ-02' group by KODE_SUPLIER;
select KODE_SUPLIER,count(JUMLAH_PASOK) as CACAH_PASOK, sum(JUMLAH_PASOK) as TOTAL_PASOK from pasok where KODE_SUPLIER in('EJ-01','EJ-02') group by KODE_SUPLIER;
select KODE_SUPLIER,count(JUMLAH_PASOK) as CACAH_PASOK, sum(JUMLAH_PASOK) as TOTAL_PASOK from pasok where KODE_SUPLIER in('EB-01','EB-02') group by KODE_SUPLIER;

-- with Union
select KODE_SUPLIER, count(JUMLAH_PASOK) as CACAH_PASOK,sum(JUMLAH_PASOK) as TOTAL_PASOK from pasok where KODE_SUPLIER='EJ-01' group by KODE_SUPLIER UNION select KODE_SUPLIER, count(JUMLAH_PASOK) as CACAH_PASOK,sum(JUMLAH_PASOK) as TOTAL_PASOK from pasok where KODE_SUPLIER='EJ-02' group by KODE_SUPLIER UNION select KODE_SUPLIER,count(JUMLAH_PASOK) as CACAH_PASOK,sum(JUMLAH_PASOK) as TOTAL_PASOK from pasok where KODE_SUPLIER in('EB-01','EB-02') group by KODE_SUPLIER;


2. INTERSECT /intersection (irisan)
irisan bertujuan mencari isisan diantara query yang ada dengan syarat adanya kesamaan domain pada kolom kolom yang ada
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SELECT         field 1,...,field n,[agregate function]
FROM         table
 [WHERE]     [criteria]
 [GROUP BY] [field 1,...,field n]
INTERSECT
SELECT         field 1,...,field n,[agregate function]
FROM         table
 [WHERE]     [kriteria]
 [GROUP BY] [field 1,...,field n]

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

select * from pasok;
select * from pasok where JUMLAH_PASOK >=4;
select * from pasok where JUMLAH_PASOK <=8;
select * from pasok where JUMLAH_PASOK >=4 INTERSECT select * from pasok where JUMLAH_PASOK <=8;

-- error di mariadb 10.1 solve di mariadb 10.5    
select KODE_BARANG,count(JUMLAH_PEMBELIAN) from pembelian group by KODE_BARANG;
select KODE_BARANG,count(JUMLAH_PASOK) from pasok group by KODE_BARANG;
select KODE_BARANG,count(JUMLAH_PEMBELIAN) from pembelian group by KODE_BARANG INTERSECT select KODE_BARANG,count(JUMLAH_PASOK) from pasok group by KODE_BARANG;


3. MINUS (difference)
minus bertujuan untuk menampilkan data hasil pengurangan dari dua query atau sub query dengan syarat mempunyai domain yang sama
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SELECT         field 1,...,field n,[agregate function]
FROM         table
 [WHERE]     [criteria]
 [GROUP BY] [field 1,...,field n]
MINUS | DIFFERENCE
SELECT         field 1,...,field n,[agregate function]
FROM         table
 [WHERE]     [kriteria]
 [GROUP BY] [field 1,...,field n]

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

-- hanya di oracle, tidak di MariaDB /MSSQL
select * from pasok;
select KODE_BARANG,count(JUMLAH_PASOK) as CACAH_PASOK,sum(JUMLAH_PASOK) as TOTAL_PASOK from pasok group by KODE_BARANG;
select KODE_BARANG,count(JUMLAH_PASOK) as CACAH_PASOK,sum(JUMLAH_PASOK) as TOTAL_PASOK from pasok having sum(JUMLAH_PASOK)>15 group by KODE_BARANG;
select KODE_BARANG,count(JUMLAH_PASOK) as CACAH_PASOK,sum(JUMLAH_PASOK) as TOTAL_PASOK from pasok group by KODE_BARANG MINUS select KODE_BARANG,count(JUMLAH_PASOK) as CACAH_PASOK,sum(JUMLAH_PASOK) as TOTAL_PASOK from pasok having sum(JUMLAH_PASOK)>15 group by KODE_BARANG;

4. DISTINCT /proyeksi 
distinct digunakan untuk menghilangkan nilai ganda
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SELECT         field 1,...,field n,[agregate function]
FROM         table
 [WHERE]     [criteria]
 [GROUP BY] [field 1,...,name_field n]

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

select KODE_BARANG,TANGGAL_PEMBELIAN,JUMLAH_PEMBELIAN from pembelian;
select DISTINCT KODE_BARANG,TANGGAL_PEMBELIAN,JUMLAH_PEMBELIAN from pembelian;

No comments:

Post a Comment

Popular Posts