Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8813355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:49:42+00:00 2026-06-14T03:49:42+00:00

Have forgotten SQL queries as have not used it for a long time. I

  • 0

Have forgotten SQL queries as have not used it for a long time.

I have a following requirement.
Have a table called match where I keep my competitor details with respect to matches my team have played against them. So some important fields are like this

match_id
competior_id
match_winner_id
ismatchtied
goals_scored_my_team
goals_scored_comp

From this table I want to get the head to head information for all my competitors.
like this

Competitor Matches Wins Losses Draws 
A          10      5    4      1
B          8       3    2      1

Draw information I can get from ismatchtied is set to ‘Y’ or ‘N’.
I want to get all the info from one query. I can get all the info from executing queries separately and do complex logic processing in my server code. But my performance will take a hit.

Any help will be hugely appreciated.

cheers,
Saurav

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:49:43+00:00Added an answer on June 14, 2026 at 3:49 am

    You could use conditional aggregation, involving CASE expressions inside aggregate functions, like this:

    SELECT
      competitor_id,
      COUNT(*) AS Matches,
      COUNT(CASE WHEN goals_scored_my_team > goals_scored_comp THEN 1 END) AS Wins,
      COUNT(CASE WHEN goals_scored_my_team < goals_scored_comp THEN 1 END) AS Losses,
      COUNT(CASE WHEN goals_scored_my_team = goals_scored_comp THEN 1 END) AS Draws
    FROM matches
    GROUP BY
      competitor_id
    ;
    

    Every CASE above will evaluate to NULL when the condition isn’t satisfied. And since COUNT(expr) omits NULLs, every COUNT(CASE ...) in the above query will effectively only count rows that match the corresponding WHEN condition.

    So, the first COUNT counts only rows where my team scored more against the competitor, i.e. where my team won. In a similar way, the second and the third CASEs get the numbers of losses and draws.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have forgotten stuff on how to compute time complexities of algorithms. I am
yikes i have forgotten but am following: http://www.symfony-project.org/getting-started/1_4/en/03-Symfony-Installation i have a jobeet directory and
Long ago I have learned sql and within the last years of application development
I've been using ORM's for so long, I appear to have forgotten most of
I have forgotten the syntax for a generic method: public static void swap <T>
have written this little class, which generates a UUID every time an object of
have 2 questions : A computer with 32-bit address uses 2-level page table (9
I have the SQL query: SELECT ISNULL(t.column1, t.column2) as [result] FROM t I need
I have forgotten my Keystore password and I don't really know what to do
This may seem silly but I seem to have forgotten the order of replacement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.