Cross Join Atau Cartesian Pada Oracle SQL

Sintak Dasar

Dibawah ini adalah sintak dasar Cross Join atau Cartesian Product pada Oracle SQL:

SELECT table1.column1, table2.column2...
FROM table1, table2 [, table3 ]

Contoh

Asumsikan Anda telah mempunyai dua tabel, yaitu tabel Customer dan tabel Orders dengan data-data sebagai berikut:

Tabel Customer

SQL> SELECT * FROM customer;

CUSTOMER_ID CUSTOMER_NAME     CUSTOMER_ADDRESS
----------- ----------------- -----------------
CS001       Nursalim          Bandung
CS002       Nani Indriyani    Jakarta
CS003       Naura Krasiva     Surabaya
CS004       Ahmad Fatoni      Semarang
CS005       Sri Rahayu        Yogyakarta

Tabel Orders

SQL> SELECT * FROM orders;

ORDER_ID ORDER_DATE CUSTOMER_ID QTY   AMOUNT
-------- ---------- ----------- ----- -------
00001    01-04-2015 CS001       2     10000
00002    08-04-2015 CS001       3     7000
00003    14-04-2015 CS004       1     15000

Untuk menggunakan Cross Join pada kedua tabel diatas, Anda dapat menggunakan query SQL di bawah ini:

SQL> SELECT customer.customer_id, customer.customer_name, orders.order_id, orders.order_date, orders.qty, orders.amount
  2  FROM customer, orders;

CUSTOMER_ID CUSTOMER_NAME      ORDER_ID ORDER_DATE QTY     AMOUNT
----------- ------------------ -------- ---------- ----- ----------
CS001       Nursalim           00001    01-04-2015     2      10000
CS002       Nani Indriyani     00001    01-04-2015     2      10000
CS003       Naura Krasiva      00001    01-04-2015     2      10000
CS004       Ahmad Fatoni       00001    01-04-2015     2      10000
CS005       Sri Rahayu         00001    01-04-2015     2      10000
CS001       Nursalim           00002    08-04-2015     3       7000
CS002       Nani Indriyani     00002    08-04-2015     3       7000
CS003       Naura Krasiva      00002    08-04-2015     3       7000
CS004       Ahmad Fatoni       00002    08-04-2015     3       7000
CS005       Sri Rahayu         00002    08-04-2015     3       7000
CS001       Nursalim           00003    14-04-2015     1      15000
CS002       Nani Indriyani     00003    14-04-2015     1      15000
CS003       Naura Krasiva      00003    14-04-2015     1      15000
CS004       Ahmad Fatoni       00003    14-04-2015     1      15000
CS005       Sri Rahayu         00003    14-04-2015     1      15000

15 rows selected.

Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment