The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calculating the average kekanap Shell Programming and Scripting 2 01-26-2007 09:36 AM
calculating size of int apoorvasharma80 High Level Programming 10 11-01-2006 06:44 PM
calculating a check digit jim9418 UNIX for Dummies Questions & Answers 1 02-16-2005 03:14 PM
Calculating Disc Space Docboyeee Filesystems, Disks and Memory 5 06-05-2002 10:06 AM
Calculating the day of the week ligs UNIX for Dummies Questions & Answers 4 10-27-2001 09:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-17-2007
figaro figaro is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 267
Calculating quantiles in SQL

SQL doesn't natively support the calculation of quantiles , so here is my attempt. Suppose there is an array of numbers x from 0 to 100 in my database. Quantile Q is calculated by dividing the following two outcomes :
- Select count(x) from data where x<Q
- Select count(x) from data
How do I combine the two queries into one outcome ?

Select count(D1.x)/count(D2.x) from data AS D1, data AS D2 where D1.x<Q and D1.x = D2.x
(assuming no duplicates)
does not yield the desired answer.
  #2 (permalink)  
Old 11-20-2007
greg greg is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 10
Hi Figaro,

the condition "D1.x<Q" of your query will prevent all the lines to be counted, that's why you don't get what you expect.

I suppose the SQL engine you dispose of is very basic, because this query is very easy to write in a Oracle database for instance. I guess it's basic standard SQL.

So I thought in such circumstances that the CASE instructions (which is part of standard SQL) can help you. Here would be the query:

SELECT SUM(1) / SUM(CASE WHEN x<Q THEN 1 ELSE 0 END)
FROM data

Is it what you expected or didn't I understand the question properly?
  #3 (permalink)  
Old 11-20-2007
greg greg is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 10
I think I inverted the sums. It should be like this:

SELECT SUM(CASE WHEN x<Q THEN 1 ELSE 0 END) / SUM(1)
FROM data
  #4 (permalink)  
Old 11-20-2007
figaro figaro is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 267
That seems to work, thanks
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0