How to find the top percentage of users

How to find the top percentage of users

Question:
I want to find out the top 5%, 10%, and 30%
customers based on their purchases. For example,
my table looks like:CustomerID Purchase(in $)
1 100
2 500
Up to 100 customers. I am getting the top 5% through:
select top 5 percent Purchase,CustomerID
from Customer
order by CustomerID desc

The same is true for the top 10%. I am getting 10 records
(assuming I have a total of 100 records).But I don’t want to see the first 5 customers,
whom I have already seen as a result of the previous
query. Is there any way I can write code
in order to see only next five records?

Answer:
The strategy is to cull from the data the records you
already saw (I stuck them in a temp table called #keeptrack)
and eliminate subsequent reads by using NOT IN the
next time around like this:

if exists (select * from sysobjects where type = "U" and name like
"#keeptrack")begin drop table #keeptrackendgoset rowcount 5--create table #keeptrack(au_lname varchar(40),
title varchar(80))goinsert into #keeptrack select top 5 percent au_lname
, title from titleview--declare @reccount intselect au_lname, title from titleview where title
not in (select title from #keeptrack) order by au_lname

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several