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

246
Views
SQL SUM and DISTINCT two columns

I have a table called points and based on distinct values of entires, I want to sum valid entries.

I have this:

valid_entries Codes
1 768
2 1224
2 1224
1 512
1 512

Based on distinct values I would have:

valid_entries codes
1 768
2 1224
1 512

I want to get the sum of valid_entries that would be 4.

I have tried this one and I have PostgreSQL 11.5 version and I am new to this. I would really appreciate it if anyone could help with this.

SELECT sum(valid_entries) FROM 
(SELECT count(distinct(codes)) FROM points WHERE  participant_id='123') t
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

First filter out the duplicate rows with DISTINCT and then aggregate:

SELECT SUM(valid_entries) total
FROM (SELECT DISTINCT valid_entries, Codes FROM points WHERE participant_id='123') t;

See the demo.

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!