Hello again,
As suggested in one of my previous messages, I'd better create a new question.
So, I was wondering if anyone can help me with this? I tried so many things found online...
I'm trying to randomly select a value depending on the values in the table. Anyone could help here?
Imagine I have a table of element like this one. A key may appear multiple times. In reality this table contains millions of keys and I will only keep the first 1000 or so.
CREATE TABLE elements
(
key varchar2(10),
value number
);
insert into elements(key, value) VALUES('a', 1);
insert into elements(key, value) VALUES('a', 2);
insert into elements(key, value) VALUES('a', 3);
insert into elements(key, value) VALUES('b', 1);
insert into elements(key, value) VALUES('b', 1);
insert into elements(key, value) VALUES('c', 1);
insert into elements(key, value) VALUES('d', 1);
insert into elements(key, value) VALUES('d', 1);
insert into elements(key, value) VALUES('d', 1);
insert into elements(key, value) VALUES('e', 1);
COMMIT;
DECLARE
TYPE string_t IS TABLE OF VARCHAR2(50) INDEX BY PLS_INTEGER;
elements_l string_t;
rnd VARCHAR2(10) ;
BEGIN
SELECT key
BULK COLLECT INTO elements_l
FROM elements;
rnd := -- get random value depending on the number of occurences that appear in my table.
DBMS_OUTPUT.put_line(rnd);
END;
/
What I would to do is to get the keys and the number of times it appears in my set (in this elements table). Then, depending on the number of times it is there, I want to randomly give it more weight.
Somehow, it's true that if I load everything, it will work. But, I wanted to avoid that... And I'm getting stuck here...
the value that should be returned should take into account the number of values in the table. For example a appears 3 times. So if I execute my function 100 times I should get a about 30 times in the result. I know how to use dbms_random to get a value between two numbers. But I have no idea how to "weight" that random with the number of occurrences.
Any help? Any suggestions?
Any advise would be more than welcome :)
Regards,