Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql select with CASE not retrieving other column value?
    primarykey
    data
    text
    <p>As the answer that I got receive from my question which drive me nuts applying to my code for some reason, why it did not worked.</p> <p>Here's a part of my data on table.</p> <p><img src="https://i.stack.imgur.com/E8ML8.png" alt="enter image description here"></p> <p><strong>Original Code:</strong></p> <pre><code>SELECT AGE_RANGE, COUNT(*) FROM ( SELECT CASE WHEN YearsOld BETWEEN 0 AND 5 THEN '0-5' WHEN YearsOld BETWEEN 6 AND 10 THEN '6-10' WHEN YearsOld BETWEEN 11 AND 15 THEN '11-15' WHEN YearsOld BETWEEN 16 AND 20 THEN '16-20' WHEN YearsOld BETWEEN 21 AND 30 THEN '21-30' WHEN YearsOld BETWEEN 31 AND 40 THEN '31-40' WHEN YearsOld &gt; 40 THEN '40+' END AS 'AGE_RANGE' FROM ( SELECT YEAR(CURDATE())-YEAR(DATE(birthdate)) 'YearsOld' FROM MyTable ) B ) A GROUP BY AGE_RANGE </code></pre> <p>and here is the result.</p> <p><img src="https://i.stack.imgur.com/GguHS.png" alt="enter image description here"></p> <p>What I'm trying to do is, I'm trying to add another column which would count on how many people who is in that area which would be <strong>location</strong> as you would see on the picture on top which includes Perth, Western Australia, Sunbury, Victoria and so on.</p> <p><strong>First attempt to fix my problem</strong></p> <p>As you would see below, I've added location, and COUNT(location) loc to get the name of the location and count on how many location that is duplicated in the table.</p> <pre><code>SELECT AGE_RANGE, COUNT(*), location, COUNT(location) loc FROM ( SELECT CASE WHEN YearsOld BETWEEN 0 AND 5 THEN '0-5' WHEN YearsOld BETWEEN 6 AND 10 THEN '6-10' WHEN YearsOld BETWEEN 11 AND 15 THEN '11-15' WHEN YearsOld BETWEEN 16 AND 20 THEN '16-20' WHEN YearsOld BETWEEN 21 AND 30 THEN '21-30' WHEN YearsOld BETWEEN 31 AND 40 THEN '31-40' WHEN YearsOld &gt; 40 THEN '40+' END AS 'AGE_RANGE', 'location' FROM ( SELECT YEAR(CURDATE())-YEAR(DATE(birthday)) 'YearsOld' FROM event_participants ) B ) A FROM event_participants WHERE location &lt;&gt; '' GROUP BY location HAVING loc &gt;= 1 ORDER BY loc DESC LIMIT 5 </code></pre> <p>Which results to..</p> <p><img src="https://i.stack.imgur.com/SWnRr.png" alt="enter image description here"></p> <p>Obviously, hearing from the aid of god doesn't work out to well..</p> <p><strong>My second attempt to fix this problem</strong></p> <pre><code>SELECT AGE_RANGE, COUNT(*), location FROM ( SELECT CASE WHEN YearsOld BETWEEN 0 AND 5 THEN '0-5' WHEN YearsOld BETWEEN 6 AND 10 THEN '6-10' WHEN YearsOld BETWEEN 11 AND 15 THEN '11-15' WHEN YearsOld BETWEEN 16 AND 20 THEN '16-20' WHEN YearsOld BETWEEN 21 AND 30 THEN '21-30' WHEN YearsOld BETWEEN 31 AND 40 THEN '31-40' WHEN YearsOld &gt; 40 THEN '40+' END AS 'AGE_RANGE', 'location' FROM ( SELECT YEAR(CURDATE())-YEAR(DATE(birthday)) 'YearsOld' FROM event_participants ) B ) A GROUP BY AGE_RANGE </code></pre> <p>And the result is...</p> <p><img src="https://i.stack.imgur.com/1BWuh.png" alt="enter image description here"></p> <p>Instead of <strong>showing</strong> the name of the countries, it only show location. </p> <p>Well.. </p> <p>Long story short, what's wrong with the code? Any solution for this stuff?</p> <p>It was like, my desired result would be,</p> <pre><code>AGE_RANGE COUNT(*) location total number of same location 16-20 4 Name of location # 21-30 45 Name of location # 31-40 79 Name of location # 40+ 102 Name of location # </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload