Calculating quantiles in SQL


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Calculating quantiles in SQL
# 1  
Old 11-17-2007
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  
Old 11-20-2007
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  
Old 11-20-2007
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  
Old 11-20-2007
That seems to work, thanks
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

2. Shell Programming and Scripting

Calculating sum

Hi all, I have the following code in a shell script. Code: if then echo "##" echo "##" echo "##################################" for fn in `find "$1" -maxdepth 1 -iname \*"$2"* -type f` do echo "$fn" ... (2 Replies)
Discussion started by: naveendronavall
2 Replies

3. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

4. UNIX for Advanced & Expert Users

Call parallel sql scripts from shell and return status when both sql are done

Hi Experts: I have a shell script that's kicked off by cron. Inside this shell script, I need to kick off two or more oracle sql scripts to process different groups of tables. And when both sql scripts are done, I will continue in the shell script to do other things like checking processing... (3 Replies)
Discussion started by: huasheng8
3 Replies

5. UNIX for Dummies Questions & Answers

Calculating average

Hi, i have 12 float variables in a bash file and i want to calculate the average of them. Can any body help? (6 Replies)
Discussion started by: limadario
6 Replies

6. Shell Programming and Scripting

Execute multiple SQL scripts from single SQL Plus connection

Hi! I would like to do a single connection to sqlplus and execute some querys. Actually I do for every query one connection to database i.e echo 'select STATUS from v$instance; exit' > $SQL_FILE sqlplus user/pass@sid @$SQL_FILE > $SELECT_RESULT echo 'select VERSION from v$instance;... (6 Replies)
Discussion started by: guif
6 Replies

7. Shell Programming and Scripting

calculating in MB

hi all, have got a ksh script which tries to monitor memory usage of app servers. i do a ps -0 rss -p <PID> to get the memory size in KB but when i divide by 1024 to convert to MB i dont know how to round it up ?? thanks in advance. (3 Replies)
Discussion started by: cesarNZ
3 Replies

8. UNIX for Dummies Questions & Answers

Execute PL/SQL function from Unix script (.sql file)

Hi guys, I am new on here, I have a function in oracle that returns a specific value: create or replace PACKAGE BODY "CTC_ASDGET_SCHED" AS FUNCTION FN_ASDSCHEDULE_GET RETURN VARCHAR2 AS BEGIN DECLARE ASDSchedule varchar2(6); ASDComplete... (1 Reply)
Discussion started by: reptile
1 Replies

9. Shell Programming and Scripting

Calling SQL LDR and SQL plus scripts in a shell script

Hi- I am trying to achieve the following in a script so I can schedule it on a cron job. I am fairly new to the unix environment... I have written a shell script that reads a flat file and loads the data into an Oracle table (Table1) via SQLLDR. This Works fine. Then, I run a nested insert... (5 Replies)
Discussion started by: rajagavini
5 Replies

10. Shell Programming and Scripting

Calculating the average

This is the cronjob ---------------------- root@a7germ:/home/paxtemp > crontab -l|grep test 57 * * * * /home/paxtemp/test_1.sh 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/paxtemp/test.sh root@a7germ:/home/paxtemp > This is the contents of test.sh script... (2 Replies)
Discussion started by: kekanap
2 Replies
Login or Register to Ask a Question