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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:59:18+00:00 2026-06-16T02:59:18+00:00

Pretend I have this table, hypothetically representing users (the voters) rating various objects (e.g.

  • 0

Pretend I have this table, hypothetically representing users (the “voters”) rating various objects (e.g. places, websites, answers, etc., the “votees”) between one to five stars (the “score”). The more generic version of this question asks what to do for one to N foos instead.

mysql> SHOW CREATE TABLE scores;
CREATE TABLE `scores` (
    `voter_id` BIGINT NOT NULL DEFAULT '0',
    `votee_id` BIGINT NOT NULL DEFAULT '0',
    `score` tinyint NOT NULL,
    PRIMARY KEY (`voter_id`,`votee_id`),
    KEY `votee_index` (`votee_id`)
);

I want to build a .csv file for each votee object, with columns representing the number of one, two, three, four, and five star votes the object has been given, e.g.

output.csv:
votee_id, count_ones, count_twos, count_threes, count_fours, count_fives
1, 3, 7, 5, 3, 2
...

I know I can get the raw data to back this table using this query:

SELECT votee_id, score, COUNT(score)
FROM scores
GROUP BY votee_id, score;

This doesn’t give me the data in the csv format I want thought:

  1. it won’t list 0’s for scores not seen for a given object.

  2. it doesn’t combine all five scores for an object into a single
    line/row
    (ie, denormalize/histogram the data).

I want to create the output using nothing but mysql.

After hacking around for a while I have the following query, which works; but it’s pretty inefficient, and I can’t find/make anything more elegant:

SELECT votee_id, GROUP_CONCAT(COALESCE(score_count, '0') ORDER BY AllUserCrossScore.score ASC)
FROM
    (SELECT votee_id, score FROM
        (SELECT 1 AS score UNION ALL
         SELECT 2 AS score UNION ALL
         SELECT 3 AS score UNION ALL
         SELECT 4 AS score UNION ALL
         SELECT 5 AS score) ScoresEnum

         JOIN

         (SELECT DISTINCT votee_id FROM scores) DistinctIds
    ) AllUserCrossScore

    LEFT JOIN

    (SELECT votee_id, score, COUNT(score) as score_count
     FROM scores
     GROUP BY votee_id, score
    ) ScoreCounts

    USING (votee_id, score)
GROUP BY votee_id;

In particular, this feels especially hacky because I use GROUP_CONCAT with ‘,’ to put the scores together; and then fiddle elsewhere to have mysql join all the other fields with ‘,’ as well, resulting in the correct formatting for the .csv (fiddling not shown).

How can I do better in a case like this?

  • 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-16T02:59:19+00:00Added an answer on June 16, 2026 at 2:59 am

    I think you are looking for something like this:

    SELECT
      votee_id
      SUM(CASE WHEN score = 1 THEN 1 ELSE 0 END) as count_ones
      SUM(CASE WHEN score = 2 THEN 1 ELSE 0 END) as count_twos
      SUM(CASE WHEN score = 3 THEN 1 ELSE 0 END) as count_threes
      SUM(CASE WHEN score = 4 THEN 1 ELSE 0 END) as count_fours
      SUM(CASE WHEN score = 5 THEN 1 ELSE 0 END) as count_fives
    FROM scores
    GROUP BY votee_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's pretend I have a large recipe-database. A table for recipes each with an
I have an object which I'm filling with objects containing 2 table rows each.
Guys this is my first question. I have a table which displays the students
So I have this website that has a search feature which searches a table
I have two tables, for simplicity lets pretend they're defined like this Stack {
I have a table of users which has a username column consisting of a
i have a form, which you can pretend is laid out like Windows Explorer:
I have an ASP.NET application that uses themes. Let's pretend I have a theme
I'm going to give this another try because my last question might have been
Lets pretend you have a 3 Gb jar file and your application only uses

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.