![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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? |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|