Data Validation!

Hi Expert,

I have a following requirement could anyone please suggest on this!

Assume a Sales table with columns for Sales_Amount, Customer_Name, and Region.
There are other columns and indeed other tables, but that’s not important.
 Give me the top 5 customers by total Sales_Amount, within each Region.
 The number of regions may vary over time, and your query needs to keep working as regions come and
 go (i.e., you don’t get to handle this with a CASE statement). You may assume there are more than
 5 customers with sales for each Region. So if I have 3 unique values of region in the table,
 I should get 15 rows, 5 for each region. But if there are 5 regions, I should get 25 rows.
 Write a Oracle-SQL script for this. No JOINs are needed, you’re only using one table.
You can use as many queries, and as many Temp Tables, as you need.


create table sales (customer_name varchar2(10), region varchar(20), amount number(10));

begin

insert into sales values ('ahmed2', 'Asia', 40000);
insert into sales values ('ahmed3', 'Asia', 60000);
insert into sales values ('ahmed4', 'Europe', 210000);
insert into sales values ('ahmed5', 'Europe', 30000);
insert into sales values ('ahmed6', 'Europe', 210000);
insert into sales values ('ahmed8', 'Europe', 5000);
insert into sales values ('ahmed9', 'Africa', 8000);
insert into sales values ('ahmed10', 'Africa', 86000);
insert into sales values ('ahmed11', 'Africa', 99000);
insert into sales values ('ahmed12', 'Australia', 76000);
insert into sales values ('ahmed13', 'Australia', 21000);
insert into sales values ('ahmed14', 'East America', 64000);
insert into sales values ('ahmed20', 'Asia', 96000);
insert into sales values ('ahmed23', 'South America', 98000);
insert into sales values ('ahmed24', 'South America', 95000);
insert into sales values ('ahmed25', 'North America', 98000);
insert into sales values ('ahmed26', 'North America', 91000);
insert into sales values ('ahmed27', 'South America', 97000);
insert into sales values ('ahmed28', 'South America', 94000);
insert into sales values ('ahmed29', 'Asia', 160000);
insert into sales values ('ahmed30', 'Asia', 620000);
insert into sales values ('ahmed31', 'Asia', 610000);
insert into sales values ('ahmed32', 'Australia', 71000);
insert into sales values ('ahmed33', 'Australia', 21000);
insert into sales values ('ahmed34', 'Australia', 51000);
insert into sales values ('ahmed38', 'North America', 108000);
insert into sales values ('ahmed39', 'North America', 38000);
insert into sales values ('ahmed40', 'North America', 68000);

end;
/

select * from sales;

Open in new window


Thanks!
MIHIR KAR#Hadoop #Oracle_DB #UNIX beginnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
This should do..
select customer_name, region, amount
FROM (
select customer_name, region, amount, row_number() over (partition by region order by amount desc) rnum 
from sales) as temp
WHERE rnum <= 5

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alexander Eßer [Alex140181]Software DeveloperCommented:
What Oracle version?
0
MIHIR KAR#Hadoop #Oracle_DB  #UNIX beginnerAuthor Commented:
Hi Alex please find the version is

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
"CORE      11.2.0.4.0      Production"
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Hi Mihir,

Kindly let me know whether you have tried the query I've provided..
0
MIHIR KAR#Hadoop #Oracle_DB  #UNIX beginnerAuthor Commented:
Hi @Raja the query Looks good for top 5 customers, but i'm not getting the below o/p!

So if I have 3 unique values of region in the table,
 I should get 15 rows, 5 for each region. But if there are 5 regions, I should get 25 rows.

Thanks!
0
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Can you kindly post the expected result set for the sample provided so that we can understand your requirement better..
0
awking00Information Technology SpecialistCommented:
>>So if I have 3 unique values of region in the table,
  I should get 15 rows, 5 for each region. But if there are 5 regions, I should get 25 rows.

Not if one region only had 3 salesmen. You sample data shows seven regions, but only Asia, Australia, and North America have at least five names, so you would get fewer than five rows from the other regions.
0
Alexander Eßer [Alex140181]Software DeveloperCommented:
Raja's post should do the job, also some of those techniques are shown here: https://oracle-base.com/articles/misc/top-n-queries
BUT keep in mind what awking00 mentioned above!!
0
MIHIR KAR#Hadoop #Oracle_DB  #UNIX beginnerAuthor Commented:
Thank you, i have checked the table and found the data are in de-normalized, i would like to go with @Raja Solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.