Skip to content

Commit 0868e4c

Browse files
authored
Create top-percentile-fraud.sql
1 parent 0771526 commit 0868e4c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

MySQL/top-percentile-fraud.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
WITH pct_rank_cte AS (
5+
SELECT *,
6+
PERCENT_RANK() OVER(PARTITION BY state ORDER BY fraud_score DESC) AS pct_rank
7+
FROM Fraud
8+
)
9+
10+
SELECT policy_id, state, fraud_score
11+
FROM pct_rank_cte
12+
WHERE pct_rank <= 0.05
13+
ORDER BY 2, 3 DESC, 1;

0 commit comments

Comments
 (0)