• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Best Way to calculate totals from query

Participant ,
Dec 02, 2008 Dec 02, 2008

Copy link to clipboard

Copied

Could someone point me in the right direction to add up my data and distinctly show it in my query?

I have a table with the following fields:
id, team_id, compname, teamname, totallost

I want to add up the "totallost" row where the "team_id" and "compname" fields are the same...then show the compname with the sum of the totallost once in my table and determine who is winning.
TOPICS
Advanced techniques

Views

495

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Dec 02, 2008 Dec 02, 2008

Copy link to clipboard

Copied

select compname, teamname, sum(totallost) as totallost
from table
group by compname, teamname

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 02, 2008 Dec 02, 2008

Copy link to clipboard

Copied

Thank you for such a quick response. I did try this before, and it does pull out the the sum for each team. How would I only display the team with the highest "totallost"? In the case of my example, I would only want to display the team "The Betters" for the competition "Test Competition" because they have the best score of "55" lost.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 02, 2008 Dec 02, 2008

Copy link to clipboard

Copied

Depends on the db. Some have a top n, feature, some have a rank function.

Google "top n your_db_name" for details.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Dec 02, 2008 Dec 02, 2008

Copy link to clipboard

Copied

select compname, teamname, sum(totallost) as totallost
from table
group by compname, teamname
order by totallost desc
limit 1

This works in PostgreSQL

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 02, 2008 Dec 02, 2008

Copy link to clipboard

Copied

LATEST
Thank you for the great help. This code works well, but is there a way to display the highest totallost and differentiate between competition names? My example is for one compname, but the table will have multiple compname's and I want to build a table showing only the highest totallost for each compname.

You guys have been a great help. I learned something new today already.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation