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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:55:44+00:00 2026-06-17T15:55:44+00:00

My table contains votes of users for different items. It has the following columns:

  • 0

My table contains votes of users for different items. It has the following columns:

id, user_id, item_id, vote, utc_time

Only id is a unique field and the combination of user_id and utc_time is probably also unique. But user can cast votes for any item many times.

A vote is not a number but rather has one of several possible values (e.g., “awful”, “bad”, “good”, “excellent”).

I need to count how many different users cast their last vote for a given #item# as “excellent”, as “good”, etc. So assuming I have only four different possible vote values, I need to get four records with the following fields:

vote, count_of_users

I understand how to count all votes, not only last votes of users:

SELECT vote, COUNT(id) FROM votes WHERE item_id=#item# GROUP BY vote;

But I cannot figure out how to count only the votes where utc_time = MAX(utc_time) for each user… Thanks for your help.

This question is connected to the previous question of mine: Select one row with MAX(column) for known other several columns without subquery

  • 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-17T15:55:45+00:00Added an answer on June 17, 2026 at 3:55 pm

    try this solution if it fits with you,

    SELECT  a.item_ID,
            SUM(CASE WHEN a.vote = 'awful' THEN 1 ELSE 0 END) awful,
            SUM(CASE WHEN a.vote = 'bad' THEN 1 ELSE 0 END) bad,
            SUM(CASE WHEN a.vote = 'good' THEN 1 ELSE 0 END) good,
            SUM(CASE WHEN a.vote = 'excellent' THEN 1 ELSE 0 END) excellent
    FROM    tableName a
            INNER JOIN
            (
                SELECT  user_ID, MAX(utc_time) max_time
                FROM    tableName
                GROUP   BY user_ID
            ) b ON a.user_ID = b.user_ID AND
                    a.utc_time = b.max_time
    -- WHERE   a.item_ID = 'valueHere'
    GROUP   BY a.item_ID
    

    UPDATE 1

    SELECT  a.item_ID,
            a.vote,
            COUNT(*) totalCount
    FROM    tableName a
            INNER JOIN
            (
                SELECT  user_ID, MAX(utc_time) max_time
                FROM    tableName
                WHERE   item_id = 'valueHere'
                GROUP   BY user_ID
            ) b ON  a.user_ID = b.user_ID AND
                    a.utc_time = b.max_time
    GROUP   BY a.vote
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My table contains votes of users for different items. It has the following fields:
A table contains four columns server, directory, usage and datetime. All servers has got
assume my table contains following columns Roll_number numeric not null Subject1 varchar(40) null Subject2
I have a table, with the following columns: id name vote Each name is
I have one table contains field named source with varchar(max). That field has following
Settings table contains always one row with lot of long-caption columns. Opening this in
I've got a question regarding a SQL-select-query: The table contains several columns, one of
I have a table with a few thousand rows. The table contains two columns,
I have a mysql database that has 2 table. the first table contains the
Say I have a voting table, where users can vote on values up, down,

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.