
September 3rd, 2012, 09:21 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 2
Time spent in forums: 15 m 56 sec
Reputation Power: 0
|
|
|
I got it down to 1 second which is acceptable though I still wish to elaborate a bit more so I can learn if this is the right way to do it or if there is another way (I read somewhere about Decision Tree Technique when Data Mining, but don't understand it):
Pre-Steps:
I got several tables (e.g. about customers)
I've got all sorts of data like eg sale figures (eg bought an iphone, htc phone on date),birthdate etc. and all sorts of other numeric values about the customer. Which I need to do calculations (eg. Count(spendings) to get total spendings between date and date for that customer) on to use in the if-statements
Now I got a decision tree structure(basically consisting of 173 if-else statements) the if statements are always a boolean value. So it's either true or false. Depending if its true or false we go to a following if statement eg.
(If if-statement 1 is true then go to if-statement 2 else go to if-statement 12)
Depending on the route it takes a certain value is added to a variable that always starts on 0.
My solution atm:
Step 1:
The way I solved it atm is that I created a view with all the values I need to check on in the if-statements. It's a pretty big view table with 30 columns of which 29 are calculated values. Only the customerid is not a calculated value.
Simple Example (my queries need a bit more tables and joins, but I hope you'll get the picture):
SELECT C.c_id,TS.total_spendings,TR_total_returns,A.age
FROM customer C
LEFT JOIN
(SELECT c_id,COUNT(spendings) AS total_spendings
FROM sales
GROUP BY TS.c_id
)TS ON TS.c_id = C.c_id
LEFT JOIN
etc.......
Step 2:
Then I made a function like this
Example:
CREATE OR REPLACE calc_group(cid integer,total_spendings bigint, total_returns bigint, age integer) RETURN $$
DECLARE
Route integer;
Val integer;
BEGIN
Route := 1;
Group := 0;
WHILE Route <> 0 LOOP
IF Route = 1 THEN
IF total_spendings > 200 THEN
Route := 2;
Group := Group + 1;
ELSE
Route := 3;
Group := Group + 100;
END IF
END IF
IF Route = 2 THEN
IF total_spendings > 500 THEN
Route := 3;
Group := Group + 20;
ELSE
Route := 3;
Group := Group + 100;
END IF
END IF
IF Route = 3 THEN
IF total_spendings > 1000 THEN
Route := 0;
Group := Group + 1000;
ELSE
Route := 0;
Group := Group + 10;
END IF
END IF
etc..... more ifs
END LOOP
RETURN group;
END;
Step 3:
Created a second view to call the customer id and function to get the group the customer belongs too.
example:
SELECT c_id,calc_group(total_spendings,total_returns,age) from view
|