Datanoob
Datanoob

Reputation: 1

how to calculate quartiles in SQL?

i am using postgresql, i have the table with nearly 1000000 rows:

  CREATE TABLE Data(
  id varchar(8) PRIMARY KEY,
  jan INT,
  feb INT,
  mar INT,
  apr INT,
  may INT,
  jun INT,
  jul INT,
  aug INT,
  sep INT, 
  oct INT,
  nov INT,
  dec INT);

And i want to get the quartiles

For example:

ID    jan  feb  mar apr may jun jul aug sep oct nov dec
A10    1    2   3    4   5   6   7   8   9  10  11  12

I want to output as:

ID    Q1  Q2  Q3 Q4
A10    3  6   9  12

How can i do that in postgresql?

Upvotes: 0

Views: 935

Answers (2)

Giorgi Nakeuri
Giorgi Nakeuri

Reputation: 35780

May be this will give you desired result:

Select id, 
       jan + feb + mar as Q1,
       apr + may + jun as Q2,
       jul + aug + sep as Q3,
       oct + nov + dec as Q4,
From Data

Upvotes: 1

SoulTrain
SoulTrain

Reputation: 1904

You could just add the columns that make up the Quarters

select id
, (case 
     when jan>feb and jan>mar then jan
     when feb>jan and feb>mar then feb
     else mar END
       ) q1
...Similarly for q2,q3,q4
from table 

Upvotes: 0

Related Questions