Home > Ask the Oracle Database / Applications Experts > Questions & Answers > Calculating quartiles
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Calculating quartiles

Jason Law EXPERT RESPONSE FROM: Jason Law

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 15 March 2002
Further to my question on percentiles, how could I return a table containing the lower and upper quartile of a particular variable?


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


This is similar to the question about percentiles, which I responded to at http://searchdatabase.techtarget.com/ateQuestionNResponse/0,289625,sid13_cid450343_tax289340,00.html. I'll use the same table and data to illustrate how to calculate quartiles. Here's the DDL:

create table ExamResults
( Student varchar(15),
  Score numeric(3)
);

insert into ExamResults values ('Lenny',94);
insert into ExamResults values ('Ralph',93);
insert into ExamResults values ('Joe',91);
insert into ExamResults values ('Mary',80);
insert into ExamResults values ('Frank',89);
insert into ExamResults values ('Susan',89);
insert into ExamResults values ('Bill',74);
insert into ExamResults values ('Ben',99);
insert into ExamResults values ('Fred',66);
insert into ExamResults values ('George',100);
insert into ExamResults values ('Walter',90);
insert into ExamResults values ('Betty',89);
insert into ExamResults values ('Heather',83);
insert into ExamResults values ('Richard',91);
From here, we can calculate the quotient of two scalar subqueries -- one to determine how many scored at or below a particular row, and another to determine how many rows exist in the set -- to determine where a particular row falls in the ranking. Multiplying that quotient by four and taking the ceiling of the result gives us our quartile.
select a.*, ceiling( 4.0 * 
                     ( select count(*) 
                         from ExamResults  
                         where Score <= a.Score ) /
                     ( select count(*) cnt 
                         from ExamResults )
                   ) quartile
  from ExamResults a;
Here are the query results:
STUDENT         SCORE QUARTILE
--------------- ----- --------
Lenny              94        4
Ralph              93        4
Joe                91        3
Mary               80        1
Frank              89        2
Susan              89        2
Bill               74        1
Ben                99        4
Fred               66        1
George            100        4
Walter             90        3
Betty              89        2
Heather            83        2
Richard            91        3

For More Information

  • What do you think about this answer? E-mail the edtiors at editor@searchDatabase.com with your feedback.
  • The Best SQL Web Links: tips, tutorials, scripts, and more.
  • Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
  • Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.




Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts