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 8295971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:47:51+00:00 2026-06-08T14:47:51+00:00

I have a table with sometable: name |test_date | score ———————– jon |2012:07:01| 95

  • 0

I have a table with sometable:

name |test_date | score   
-----------------------
jon  |2012:07:01|   95   
jon  |2012:07:01|   60   
jon  |2012:07:01|   30   
alex |2012:07:01|   80   
alex |2012:07:01|   85   
alex |2011:05:01|   40  
emile|2011:01:01|   89    

I want to get some rows for each name and give information about his score_grading with rules:
score>79=A,
80>score>49=B
otherwise C.

The problem is, I want: If in the same day there are more than one same score_grade for a student, than it will count as one score_grade.
For example, in the table we can see that alex get A two times in one day, I want it to be counted as only 1 A.

So the result will be

name |    A   |    B     |    C
jon  |    1   |    1     |    1
alex |    1   |    0     |    1  
emily|    1   |    0     |    0

I only know code like:

SELECT name,
SELECT SUM(IF(score)>79),1,0)) as A,
SELECT SUM(IF(80>score>49),1,0)) as B,
SELECT SUM(IF(score)<50),1,0)) as C from sometable group by name

Nha, how do I put a “DISTINCT” on it?
Can anybody give a solution? Maybe it does not need a DISTINCT?
Thanks. ^^

  • 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-08T14:47:52+00:00Added an answer on June 8, 2026 at 2:47 pm

    You mean something like the following (sqlfiddle)?

    SELECT name, SUM(IF(A>=1,1,0)) as A, SUM(IF(B>=1,1,0)) as B, 
        SUM(IF(C>=1,1,0)) as C
    FROM
    (
        SELECT name, test_date, SUM(IF(score>79,1,0)) as A, 
            SUM(IF(score BETWEEN 49 AND 79,1,0)) as B, SUM(IF(score<50,1,0)) as C 
        FROM sometable 
        GROUP BY name, test_date
    ) daygroups
    GROUP BY name
    

    This first shuffles your data into rows of name, test_date, A, B, C in the subquery. Then the outer query will aggregate these rows, taking a 1 if there is at least one score of that letter grade on the day, else taking a 0.


    This ought to work too (sqlfiddle):

    SELECT name, SUM(IF(lettergrade = 'A',1,0)) AS A, 
        SUM(IF(lettergrade = 'B',1,0)) AS B, SUM(IF(lettergrade = 'C',1,0)) AS C
    FROM
    (
        SELECT DISTINCT name, test_date, 
            CASE WHEN score>79 THEN 'A' 
                 WHEN score BETWEEN 49 AND 79 THEN 'B' 
                 ELSE 'C' 
            END AS lettergrade
        FROM sometable
    ) lettergrades
    GROUP BY name
    

    I’m not sure which one would be better. This query uses a DISTINCT as you suggest in your question. First it resolves each numeric grade into the corresponding letter grade, then DISTINCTs out the duplicates. Finally it shuffles the data into columns.

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

Sidebar

Related Questions

I have a table sometable: name | math | history | status ----------------------------- jon
I have a table with these fields: ID (Primary key) Name Some more data
I have two classes. I want to map Class1 to a database table with
I have the table which list some details from database.I want to sort the
I want tho have I18N categories table. I have followed the jobeet example .
Name ExactDate Presents Location bob1 2011 1 home bob2 2008 2 school bob2 2012
I have some table cells displaying different time. I want the time to be
I have some table 'TableName' like Id,Name: 1 | something 2 | _something 3
I have the name of an index that is existing on some table on
Database Name is ONLINEEXAM I have several tables in the db and I want

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.