Solved

How do I calculate quartiles in MSSQL

Posted on 2010-09-06
10
3,169 Views
Last Modified: 2012-05-10
How do I calculate a quartile in mssql (1st quartile, 2nd quartile, etc..)?
I have attached an excel sheet with 57 rows and showing what the 1st ,2nd 3rd and 4th quartile should be
Assume I have those 57 rows in mssql table
How do I get the same number from an mssql query?
Ideally I would run a query against teh database table and the result will be a table with 4 rows
(1st,2nd,3rd, and 4th quartile with their values)
Thank you

Book1.xls
0
Comment
Question by:xav056
  • 4
  • 4
  • 2
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33610235
I presume you want this:
with min_max as ( 

  select min(yourfield) n

       , max(yourfield) x

       , ( max(yourfield) - min(yourfield)  ) / 4 q 

       , min(yourfield) + 1 * ( ( max(yourfield) - min(yourfield)  ) / 4 ) q1

       , min(yourfield) + 2 * ( ( max(yourfield) - min(yourfield)  ) / 4 ) q2

       , min(yourfield) + 3 * ( ( max(yourfield) - min(yourfield)  ) / 4 ) q3

from yourtable)

select min(case when yourfield >= q1 then yourfield) Q1

     , min(case when yourfield >= q2 then yourfield) Q2

     , min(case when yourfield >= q3 then yourfield) Q3

     , min(case when yourfield >= x then yourfield) Q4

  from yourtable

Open in new window

0
 
LVL 9

Author Comment

by:xav056
ID: 33610306
getting a syntax error
q1, q2 .. in the second block
I guess because q1 q2 are in min_max
I am not sue how to fix it
thanks
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33610325
yes, indeed, sorry for that
with min_max as ( 

  select min(yourfield) n

       , max(yourfield) x

       , ( max(yourfield) - min(yourfield)  ) / 4 q 

       , min(yourfield) + 1 * ( ( max(yourfield) - min(yourfield)  ) / 4 ) q1

       , min(yourfield) + 2 * ( ( max(yourfield) - min(yourfield)  ) / 4 ) q2

       , min(yourfield) + 3 * ( ( max(yourfield) - min(yourfield)  ) / 4 ) q3

from yourtable)

select min(case when yourfield >= q1 then yourfield) Q1

     , min(case when yourfield >= q2 then yourfield) Q2

     , min(case when yourfield >= q3 then yourfield) Q3

     , min(case when yourfield >= x then yourfield) Q4

  from yourtable

  cross join min_max 

Open in new window

0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33610329
;with tmp as (
      select n, qt=NTILE(4) over (order by n), rn=ROW_NUMBER() over (order by n)
      from quartile_test)
select n
from tmp
where rn in (select MAX(rn) from tmp group by qt)


Quartile_test should be the name of your table, n is the column name.
0
 
LVL 9

Author Comment

by:xav056
ID: 33610349
I am getting
39.314516      59.208731        84.942085                          100.000000
instead of
39.9103139      53.6809816       92.28855721       100
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33610391
Were you referring to http:#a33610329?
With your data (Excel), it gives me

39.9103139013452
53.680981595092
92.2885572139303
100
0
 
LVL 9

Author Comment

by:xav056
ID: 33610497
cyberwiki:
I am getting slightly different numbers, for the rows below
17.32522796
17.97385621
25.45180723
31.39534884
32.86852590
32.95454545
36.97916667
37.84277879
39.31451613
40.92409241
41.59445407
49.30069930
50.59021922
52.03915171
53.68098160
57.50853242
59.20873124
59.49554896
60.35665295
64.09168081
70.95238095
84.94208494
92.28855721
92.28971963
92.45689655
93.47442681

I am getting
36.97916667                    52.03915171               64.09168081                 93.47442681
while excel is giving
37.1950697                         51.31468547                   63.15792385               93.47442681
the only extra thing I did was added a partition by colum2 before the order by for the ntile function, and the row number
as my original result set had 2 columns
where rows were seperated by groups of column 2

Any idea why that might be happening?
Thank you
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33610865
The reason is that Excel takes the quartiles at predetermined locations.
For your original 57-row query, the quartiles are at positions (57-1)/4 = 14 apart.  Or rather, the layout is

Ignoring 1, and adding 14 for each quartile, the positions are 15, 29, 43, 57

Using your new data with 26 elements, the quartiles should be (26-1)/4 = 6.25 apart, or positions

7.25, 13.5, 19.75, 26

There is no item at position 7.25, so instead that is calculated from (7th x 0.75 + 8th x 0.25)
Whether Excel's interpretation is correct or not is up for debate.
0
 
LVL 9

Author Comment

by:xav056
ID: 33612280
Although its still not crystal clear on how excel is calculating the position, is there an easy way to get exact results I am getting in excel using sql
thank you
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33613648

;with tmp as (

	select c=COUNT(*) over (), rn=ROW_NUMBER() over (order by n), n

	from quartile_test)

,qs as (

	select

		q1=1+(c-1.0)/4, q1a=FLOOR(1+(c-1.0)/4), q1b=CEILING(1+(c-1.0)/4),

		q2=1+(c-1.0)/2, q2a=FLOOR(1+(c-1.0)/2), q2b=CEILING(1+(c-1.0)/2),

		q3=1+(c-1.0)*3/4, q3a=FLOOR(1+(c-1.0)*3/4), q3b=CEILING(1+(c-1.0)*3/4)

	from (select top 1 c from tmp) x)

select (1-q1+q1a)*MIN(n)+(q1-q1a)*MAX(n)

from tmp,qs where rn in (q1a,q1b) group by qs.q1,qs.q1a,qs.q1b

union all

select (1-q2+q2a)*MIN(n)+(q2-q2a)*MAX(n)

from tmp,qs where rn in (q2a,q2b) group by qs.q2,qs.q2a,qs.q2b

union all

select (1-q3+q3a)*MIN(n)+(q3-q3a)*MAX(n)

from tmp,qs where rn in (q3a,q3b) group by qs.q3,qs.q3a,qs.q3b

union all

select n

from tmp,qs where rn=c

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now