on 03-07-2017 5:09 PM
Hi to everyone,
I have a huge table and I want to create new views which are the result of filtering over the distinct values of one columns. After, I want to run a procedure to each of the new filtered tables.
First, I could create a new table with the distinct values and then run a loop to create the new views (as much as the number of distinct values). At every step, I could also run a procedure. I tried the following code (which does not run).
-- Drop existing tables
DROP VIEW "FILTER";
DROP VIEW "FILTERED_DATA";
-- Find unique values in a column/s
CREATE VIEW "FILTER" AS
SELECT DISTINCT "/BIC/S_WERKS","/BIC/S_MAT"
FROM "BW_SAP"
order by "/BIC/S_WERKS", "/BIC/S_MAT"
;
CNTR := 0
-- Loop through the FILTER VIEW to Filter BW_SAP Table
WHILE CNTR < :NMBRROWS DO
CREATE VIEW "FILTERED_DATA" AS
SELECT "/BIC/S_WERKS","/BIC/S_MAT"
FROM "BW_SAP"
order by "/BIC/S_WERKS", "/BIC/S_MAT"
;
CALL "ASJ"."AFM.PAL::LOVELY_CAT"();
END WHILE;
I managed to generate the distinct values tables but not to pass each row value to the loop.
Then, I tried to use cursors, but again I failed when running them. I attached two examples:
CURSOR "FILTERED"
FOR
SELECT DISTINCT "/BIC/S_WERKS","/BIC/S_MAT"
FROM "ASJ"."BW_SAP"
order by "/BIC/S_WERKS", "/BIC/S_MAT"
;
BEGIN
FOR "FILTERED" AS FILTERED(:S_WERK,:S_MAT) DO
DROP VIEW "ASJ"."V_TS_DATA"
CREATE VIEW "ASJ"."V_TS_DATA" AS <br> SELECT "CAL_ID", SUM("/BIC/S_HIST") AS "/BIC/S_HIST"
FROM "ASJ"."BW_SAP"
WHERE "/BIC/S_WERKS" = :S_WERK AND "/BIC/S_MAT" = :S_MAT
GROUP BY "CAL_ID"
ORDER BY "CAL_ID"
;
CALL "ASJ"."AFM.PAL::LOVELY_CAT"();
END FOR;
END;
CREATE PROCEDURE MY_CURSOR_PROC ()
LANGUAGE SQLSCRIPT
AS
CURSOR my_cursor
FOR
SELECT DISTINCT "/BIC/S_WERKS","/BIC/S_MAT"
FROM "ASJ"."BW_SAP"
order by "/BIC/S_WERKS", "/BIC/S_MAT"
;
BEGIN
FOR mycursor_row AS my_cursor(:S_WERK,:S_MAT) DO
DROP VIEW "ASJ"."V_TS_DATA"
CREATE VIEW "ASJ"."V_TS_DATA" AS
SELECT "CAL_ID", SUM("/BIC/S_HIST") AS "/BIC/S_HIST"
FROM "ASJ"."BW_SAP"<br> WHERE "/BIC/S_WERKS" = :S_WERK AND "/BIC/S_MAT" = :S_MAT
GROUP BY "CAL_ID"
ORDER BY "CAL_ID"
;
CALL "ASJ"."AFM.PAL::LOVELY_CAT"();
END FOR;
END;
Any advise on what I am doing wrong or any light to a new approach will be very helpfull.
Thank you all.
AlexS
Honestly, I still don't know what you are actually trying here.
Instead of creating and dropping views over and over again, why not just use parameters in your cursor?
Or you could create a single table function that takes your filter criteria as parameters and returns the aggregated result set.
I don't know what "LOVELY_CAT()" does, but it seems to expect data in a specific table or view. Can't you provide it with parameters to enable filtering?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Dear Lars,
Thanks your for your answer. I want to run a statistical forecast for a lot of products using a HANA SQL procedure calling a AFL time series statistical function. The problem is the AFL functions only allow to run the function for one product at a time. A similar problem was reported by Nameeth Raman March 31, 2015 in his post Very log run time when using AFL-PAL functions to generate statistical forecast for 10,000 individual products (https://archive.sap.com/discussions/thread/3719685). I got stack in earlier steps when trying to apply the cursor to jump from one product forecast to the next. Nevertheless, I am not sure that using cursors is a good idea since it HANA is not very efficient at looping though data.
Thanks,
AlexS
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.