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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:21:25+00:00 2026-05-12T13:21:25+00:00

I tried different approaches but even though I think my syntax is ok the

  • 0

I tried different approaches but even though I think my syntax is ok the query doesn’t work.

I have a fairly complicated polls system. In general every answer to poll is putted into one table

CREATE TABLE `polls` (
  `poll_id` int(10) NOT NULL auto_increment,
  `client_id` int(10) NOT NULL,
  `key` varchar(20) NOT NULL,
  `val` varchar(30) NOT NULL,
  PRIMARY KEY  (`poll_id`)
);

The ‘key’ column is like ID of certain question (like for ‘How often do you drink coffee’ the key would be ‘coffee’), the value varies depending on the question – sometimes is 0 or 1, sometimes is 0-6, sometimes it’s some string.
For statistical purposes I want to display the percentage of every answer for every question. I use this query for that:

SELECT p.`key`, p.val, COUNT(p.poll_id) AS people, 
(SELECT COUNT(client_id) FROM clients) AS total 
FROM polls AS p GROUP BY p.`key`, p.val ORDER BY p.`key`, p.val

The problem in this query is that I depend on clients table to get the total count of users – which worked when every user answered every question. Now we added one more question to the poll and the query doesn’t work how it should, because now not every user answered that question but the percentage is counted like every user did answer.

I tried modifying the query to something like this (I tried different ways, but let’s say came to that):

SELECT p.`key`, p.val, COUNT(p.poll_id) AS people, 
(SELECT COUNT(p2.poll_id) FROM polls AS p2 WHERE p2.`key` = p.`key`) AS total 
FROM polls AS p GROUP BY p.`key`, p.val ORDER BY p.`key`, p.val

The way I see it – it should get all the values from the main query, group it and then execute the subquery – so I would get something like:

coffee , 0 , 10 , 30
coffee , 1 , 15 , 30
coffee , 2 , 5 , 30
tea , 0 , 5 , 25
tea , 1 , 5 , 25
tea , 3 , 5 , 25
tea , 4 , 10 , 25

The last column would count all rows that have the same key and ignore the val column (for coffee -> 10 + 15 + 5 = 30, for tea -> 5 + 5 + 5 + 10 = 25). The query doesn’t execute. It’s running and running and running and eventually I have to cancel it. I sense that the problem is in the “p2.key = p.key” part of the subquery, because if I write “p2.key = ‘coffee'” the query runs like a charm.

  • 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-12T13:21:25+00:00Added an answer on May 12, 2026 at 1:21 pm

    it’s not an actual answer to my question but since there’s no answer – maybe it’s impossible to do what I want in just one query. but it’s possible to do something similar:

    SELECT p.`key`, p.val, COUNT(p.poll_id) AS people 
    FROM polls AS p GROUP BY p.`key`, p.val WITH ROLLUP
    

    it will sum values with the same given GROUP BY columns and add the total count with grouped column = NULL. the result for my problem would look like this:

    coffee , 0 , 10
    coffee , 1 , 15
    coffee , 2 , 5
    coffee , NULL , 30
    tea , 0 , 5
    tea , 1 , 5
    tea , 3 , 5
    tea , 4 , 10
    tea , NULL , 25
    NULL , NULL , 55
    

    I’ve had to change implementation of my app to match what it was getting from database but it works for me.

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

Sidebar

Related Questions

I have tried different methods and none of them seem to work. When I
I have searched here on SO and tried different solutions offered here, but cannot
I tried different join types but couldn't figure out which type should I use.
I have a small problem, I have tried different solutions to add a image
I've searched and searched and tried many different ways, but I can't seem to
I'm trying to do something that I think should be fairly simple but I've
I have a problem I tried solving on my own, but didn't get far
I'm not exactly sure how to properly debug this but have tried a few
Last night and tonight I tried a few different approaches and came up with
I've tried different variations of the following statement, the result is half of what

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.