Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 3548

Re: Difficult to decide on which way is better

$
0
0

Hello Rithika,

 

I'm guessing you don't actually need cursor for what you're trying to achieve as you can try using window functions to help you with that. I believe using WITH clause might be a way out as well. Hopefully I understood your scenario the right way.

 

I don't have your data but using the classic abap sflight data I could come up with something that you can translate to your reality and evaluate if it makes sense or not. If that's the best approach I can't confirm. I'd avoid cursors though.

 

Anyhow, the idea was to use table SBOOK as input and play around with the FLDATE column (which you can use it as your part number column) and I counted the number of bookids for each one of the calendar month (essentially yyyymm from FLDATE).

 

After calculating the individual counts (on variable v1), the total counts (on variable v2) and the percentage (on varibale v3) you can use a percent_rank window function over the percentage calculated before in order to evaluate where does the value fit (the first 80% of results, the next 15% and the remaining 5%):

 

DO BEGIN  v1 = select left(replace(FLDATE,'-',''),6) as calmonth, count(BOOKID) as individual_cnt
from SAPABAP1.SBOOK group by left(replace(FLDATE,'-',''),6);  v2 = select sum(individual_cnt) as total from :v1;  v3 = select a.CALMONTH, a.INDIVIDUAL_CNT, b.total, round(to_decimal(a.INDIVIDUAL_CNT/b.total)*100,4) as perc
from :v1 a cross join :v2 b;  select CALMONTH, INDIVIDUAL_CNT, PERC, RANK,
(CASE WHEN RANK<=0.8 THEN 'Fast'
WHEN RANK > 0.8 and RANK <= 0.95 THEN 'Medium'
ELSE 'Slow' END) as RANK_STR
from (select CALMONTH, INDIVIDUAL_CNT, PERC, percent_rank() over (order by perc desc) as rank
from :v3 order by perc desc);
END;

This will result in something like:

 

CALMONTH;INDIVIDUAL_CNT;PERC  ;RANK                 ;RANK_STR
201307  ;261595        ;0.6262;0.0                  ;Fast  
201308  ;261494        ;0.626 ;0.0055248618784530384;Fast  
200608  ;261482        ;0.626 ;0.0055248618784530384;Fast  
200307  ;261419        ;0.6258;0.016574585635359115 ;Fast  
[...]
200909  ;229991        ;0.5506;0.7955801104972375   ;Fast  
201409  ;229972        ;0.5505;0.8121546961325967   ;Medium
201406  ;229940        ;0.5504;0.8176795580110497   ;Medium
200806  ;229872        ;0.5503;0.8232044198895028   ;Medium
201209  ;229796        ;0.5501;0.8287292817679558   ;Medium
[...]
201607  ;180380        ;0.4318;0.9447513812154696   ;Medium
201606  ;120685        ;0.2889;0.9502762430939227   ;Slow  
201603  ;85959         ;0.2057;0.9558011049723757   ;Slow  
[...]
199504  ;1             ;0     ;0.9834254143646409   ;Slow 

Took a little less than 5s to finish the results for a 4M record table on SP12. I didn't spent too much time looking at resource consumption for this query but it might be a greedy one.

 

I tried to think on the graphical way of doing that but so far could not think of anything easy.

 

I suggest you to check WITH clause and other window functions that might be worth using here.

 

ps.: I guess you can use SUM as window function as well and re-use one of the other window functions here possibly subquerying your way to one single query. Haven't spent much time on it though.

 

BRs,

Lucas de Oliveira


Viewing all articles
Browse latest Browse all 3548

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>