Ceriwis  

Go Back   Ceriwis > HOBI > Komputer & Teknologi > Programming

Programming Share, tanya jawab, saling bantu antar programmer dengan berbagai macam bahasa pemrograman.

Reply
 
Thread Tools
  #1  
Old 20th November 2011
Linuxmania Linuxmania is offline
Ceriwiser
 
Join Date: Nov 2011
Posts: 392
Rep Power: 14
Linuxmania mempunyai hidup yang Normal
Default Help, terutama yg jago oracle *cendol inside*

Bro, gw mau nanya nih. Misalnya gw ada table gini :




Code:

nama | nama_mantan_cewek
----------------------------
paijo | intan
paijo | dewi
koplo | tyas
koplo | andin
paijo | yeta
paijo | inem

nah, dari table itu gw mo bikin tabel baru dengan hasil gini :




Code:

nama | nama_mantan_cewek
-----------------------------
paijo | intan,dewi,yeta,inem
koplo | tyas,andin

kira2 gmana ya bro caranya? jangan bilang sorting manual, karena data aslinya bisa mencapai 10 juta record. Thx sebelumnya.



NB:melon menanti



CASE CLOSED!!!

Thx bwat bro postthink atas jawabannya. melon sudah dikirim.




Spoiler for berikut jawabannya:







Code:

CREATE TABLE TABEL1
(
NAMA VARCHAR2(2000 BYTE),
NAMA_MANTAN_CEWEK VARCHAR2(2000 BYTE)
)
TABLESPACE USERS
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;

CREATE TABLE TABEL2
(
NAMA VARCHAR2(2000 BYTE),
NAMA_MANTAN_CEWEK VARCHAR2(2000 BYTE)
)
TABLESPACE USERS
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;


SET DEFINE OFF;
Insert into TABEL1
(NAMA, NAMA_MANTAN_CEWEK)
Values
('paijo', 'intan');
Insert into TABEL1
(NAMA, NAMA_MANTAN_CEWEK)
Values
('paijo', 'dewi');
Insert into TABEL1
(NAMA, NAMA_MANTAN_CEWEK)
Values
('koplo', 'tyas');
Insert into TABEL1
(NAMA, NAMA_MANTAN_CEWEK)
Values
('koplo', 'andin');
Insert into TABEL1
(NAMA, NAMA_MANTAN_CEWEK)
Values
('paijo', 'yeta');
Insert into TABEL1
(NAMA, NAMA_MANTAN_CEWEK)
Values
('paijo', 'inem');
COMMIT;



lalu bikin function untuk mendapatkan nama mantan cewek

CREATE OR REPLACE FUNCTION get_mantan_cewek(nama_ in varchar2) RETURN VARCHAR2
IS
mantan_cewek varchar2(2000);
i number(10);
cursor cur is select nama_mantan_cewek from tabel1 where nama=nama_;
BEGIN
i:=1;
FOR cew in cur
Loop
if i=1 then
mantan_cewek := cew.nama_mantan_cewek;
else
mantan_cewek := mantan_cewek || ',' || cew.nama_mantan_cewek;
end if;
i:=i+1;
end loop;
return mantan_cewek;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END get_mantan_cewek;



Lalu bikin procedure untuk insert ke tabel2
CREATE OR REPLACE PROCEDURE insert_tabel2 IS
mantan varchar2(2000);
cursor cur is select distinct(nama) as nama from tabel1;
BEGIN
FOR cew in cur
Loop
mantan := get_mantan_cewek(cew.nama);
insert into tabel2 values(cew.nama,mantan);
end loop;
commit;
END insert_tabel2 ;

Jalankan procedure nya
SQL>exec insert_tabel2;

Lalu lihat hasilnya
select * from tabel2;

Kasih melon gan ya











Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


 


All times are GMT +7. The time now is 01:13 AM.


no new posts