Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

348
Views
How to get the frequency in postgresql

I have a table (table_1) like this (I have simplified it)

   code   |    description   |    season    
----------+------------------+--------------
   500    |      info 1      |    fall    
   500    |      info 4      |    fall    
   500    |      info 8      |    fall    
   500    |      info 1      |    winter
   300    |      info 1      |    spring
   400    |      info 1      |    fall

And I want a table like below, where I have the frequency of codes in each season


  season  |  Number of Unique Codes  
----------+------------------------
   fall   |           2      
  winter  |           1           
  spring  |           1

So far I have this:

SELECT 
    season, 
    count(DISTINCT code) AS "Number of Unique Codes"
FROM table_1
WHERE code IS NOT NULL
GROUP BY season
ORDER BY code desc;

However, I am running into a few issues.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your error is on the ORDER BY, change your ORDER BY to sort by the alias created.

SELECT 
    season, 
    count(distinct code) AS "Number of Unique Codes"
FROM table_1
WHERE code IS NOT NULL
GROUP BY season
ORDER BY "Number of Unique Codes" DESC;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!