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

Re: How to find top x percent rows based on a column with numerical values?

$
0
0

You might want to make yourself familiar with the window functions Window Functions - SAP HANA SQL and System Views Reference - SAP Library of SAP HANA.

 

As I am not  sure about your use case, here's an example I fiddled together years ago. It's to find out what threshold to pick for long running statements trace. There you want to focus on the top 5 percent (or so) of statements that run slow.

 

What time to pick for expensive statements trace?

 

select n_tile*25 || '% of all statements' as statment_group,       max(avg_execution_time) as upper_runtime_boundary
from (       select           avg_execution_time,           ntile (4) over (order by  avg_execution_time) n_tile       from "PUBLIC"."M_SQL_PLAN_CACHE")
group by n_tile
order by n_tile;
STATMENT_GROUP          UPPER_RUNTIME_BOUNDARY
25% of all statements   0                    
50% of all statements   943                  
75% of all statements   14.369               
100% of all statements  21.395.089   

So the ntile buckets the data into n buckets. From here it shouldn't be difficult to get to what you asked for.

 

Other than that, I strongly recommend to actually do the search-first-then-post exercise as this is a pretty standard question for SQL programming.

 

Cheers,

Lars


Viewing all articles
Browse latest Browse all 8944

Trending Articles



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