You are on page 1of 3

3/14/24, 8:31 AM SAP HANA PAL – K-Means Algorithm or How to do Cust...

- SAP Community

Generate the PAL procedure

Now that I have my dataset, I’m ready to start coding. The first thing we need to do is generate the PAL procedure by
calling the AFL Wrapper Generator. To do so we need to create a number of Table Types that will be used to define the
structure of the data that will be used as input and output parameters:

SET SCHEMA _SYS_AFL;

/* Table Type that will be used as the output parameter

that will contain which cluster has been assigned to each

customer and what is the distance to the mean of the cluster */

DROP TYPE PAL_KMEANS_RESASSIGN_TELCO;

CREATE TYPE PAL_KMEANS_RESASSIGN_TELCO AS TABLE(

"ID" INT,

"CENTER_ASSIGN" INT,

"DISTANCE" DOUBLE

);

https://community.sap.com/t5/technology-blogs-by-members/sap-hana-pal-k-means-algorithm-or-how -to-do-customer-segmentation-for-the/ba-p/12976696/page/2 6/39


3/14/24, 8:31 AM SAP HANA PAL – K-Means Algorithm or How to do Cust... - SAP Community

primary key("ID")

);

/* Table Type that will be used as the output parameter

that will contain the centers for each cluster */

DROP TYPE PAL_KMEANS_CENTERS_TELCO;

CREATE TYPE PAL_KMEANS_CENTERS_TELCO AS TABLE(

"CENTER_ID" INT,

"V000" DOUBLE,

"V001" DOUBLE,

"V002" DOUBLE,

"V003" DOUBLE,

"V004" DOUBLE,

"V005" DOUBLE,

https://community.sap.com/t5/technology-blogs-by-members/sap-hana-pal-k-means-algorithm-or-how -to-do-customer-segmentation-for-the/ba-p/12976696/page/2 8/39


3/14/24, 8:31 AM SAP HANA PAL – K-Means Algorithm or How to do Cust... - SAP Community

and will point the AFL Wrapper Generator to the different

table types that we just created */

DROP TABLE PDATA_TELCO;

CREATE COLUMN TABLE PDATA_TELCO(

"ID" INT,

"TYPENAME" VARCHAR(100),

"DIRECTION" VARCHAR(100) );

/* Fill the table */

INSERT INTO PDATA_TELCO VALUES (1, '_SYS_AFL.PAL_KMEANS_DATA_TELCO', 'in');

INSERT INTO PDATA_TELCO VALUES (2, '_SYS_AFL.PAL_CONTROL_TELCO', 'in');

INSERT INTO PDATA_TELCO VALUES (3, '_SYS_AFL.PAL_KMEANS_RESASSIGN_TELCO', 'out');

INSERT INTO PDATA_TELCO VALUES (4, '_SYS_AFL.PAL_KMEANS_CENTERS_TELCO', 'out');

/* Creates the KMeans procedure that executes the KMeans Algorithm */

https://community.sap.com/t5/technology-blogs-by-members/sap-hana-pal-k-means-algorithm-or-how -to-do-customer-segmentation-for-the/ba-p/12976696/page/2 10/39

You might also like