0

I have the following SQL statement:

SELECT wn_sum, COUNT(*) as count FROM cash_numbers GROUP BY wn_sum ORDER BY wn_sum ASC;

which does pretty much what I want but I like to select 1st 100 rows from wn_sum, or last 100 rows from wn_sum.

How would I rewrite this statement to only get first 100 rows from wn_sum or last 100 rows from wn_sum to get my counts?

mysql> SELECT wn_sum, COUNT(*) as count FROM cash_numbers GROUP BY wn_sum ORDER BY wn_sum ASC;

+--------+-------+
| wn_sum | count |
+--------+-------+
|     54 |     2 |
|     56 |     1 |
|     61 |     1 |
|     65 |     1 |
|     66 |     1 |
|     68 |     1 |
|     70 |     1 |
|     71 |     1 |
|     72 |     1 |
|     73 |     2 |
|     76 |     4 |
....
|    234 |     1 |
|    237 |     1 |
|    241 |     1 |
|    244 |     1 |
|    251 |     2 |
|    267 |     1 |
+--------+-------+
156 rows in set (0.00 sec)

mysql> 

I did try to put limit 100, but my syntax is wrong so I get errors when I try to use those limits.

nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
Chris W
  • 1
  • 1
  • 1
  • You may find the answer here, https://stackoverflow.com/questions/2129693/using-limit-within-group-by-to-get-n-results-per-group – zinmyintnaung Jun 14 '19 at 02:50
  • 1
    Probably can post the query you tried that had error so everyone can help? – ipohfly Jun 14 '19 at 04:31
  • Does `wn_sum` has repeating value? I can not see in example data. If not you really don't need `GROUP BY` – Jimish Gamit Jun 14 '19 at 04:48
  • What is the error message when you add `LIMIT 100` to the query? Because the query look very basic and adding `LIMIT 100` won't produce any error unless your query is more than that (which include some non-supporting LIMIT operation) or the position of your `LIMIT 100` is wrong. – FanoFN Jun 14 '19 at 05:38

0 Answers0