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

  • SEARCH
  • Home
  • 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 4085372
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:34:05+00:00 2026-05-20T18:34:05+00:00

I am trying to query tables using join but I can’t get success here

  • 0

I am trying to query tables using join but I can’t get success here my tables

Score
id    ,  user_id    ,     score  ,   week
1     ,   123       ,      2     ,    1
2     ,   432       ,      2     ,    1  
3     ,   432       ,      3     ,    1
4     ,   123       ,      2     ,    2

I want to query top twenty users records on the bases of sum of week 1 and week 2’s max total

I want the result should be

id      , user_id     , week1score    , week2score
1       ,  123        ,  2            ,  2
2       ,  432        ,  3            ,  0

The formula is order by descending sum(max(week1)+max(week2))

Thanks
Inam

  • 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-05-20T18:34:06+00:00Added an answer on May 20, 2026 at 6:34 pm

    something like this would do

    SELECT t1.user_id, 
           SUM(week1score) AS week1score, 
           SUM(week2score) AS week2score 
    FROM   (SELECT user_id 
            FROM   `table` 
            GROUP  BY user_id) AS t1 
           JOIN (SELECT user_id, 
                        Max(IF(`week` = 1, score, 0)) AS week1score, 
                        Max(IF(`week` = 2, score, 0)) AS week2score 
                 FROM   `table` 
                 GROUP  BY user_id, 
                           `week`) t2 
             ON t2.user_id = t1.user_id 
    GROUP  BY user_id 
    ORDER  BY ( SUM(week1score) + SUM(week2score) ) DESC 
    

    You need a composit index on the following columns

    • (user_id,week,score)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using MySQL. I have 3 tables I'm trying to connect in a query
What I'm trying to achieve must be simple but I just can't get it
I have a strange problem with my query trying to join 3 tables. The
I have a three different tables that I'm trying to join up correctly but
I am new to using Criteria, and i am trying to join tables on
I've been trying for hours but can't ge the query, i want to do
I am using following tables... stats (id, game, user_id, rank, score, dnt) teams(id, name,
I'm trying to optimize query performance and have had to resort to using optimizer
When trying to enter a SQL query with parameters using the Oracle OLE DB
I'm trying to join two SQL tables, parent (which I have complete design control

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.