Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Select only percentage of rows

Tags:

sql

mysql

I want to use the second column (art_count) to show only those rows containing X percent of total art_count.

My data:

title   art_count
a       3
b       12
c       9
d       4
e       45

My query so far:

SELECT title, COUNT(art) AS art_count
FROM table1
GROUP BY art HAVING ... ?

Tried it with SUM without success.

like image 267
user3481307 Avatar asked Dec 15 '25 01:12

user3481307


1 Answers

SELECT title, COUNT(art) AS art_count
FROM table1
GROUP BY art 
HAVING art_count >= (select count(*) * X / 100 from table1)

You need to insert a value for X

like image 91
juergen d Avatar answered Dec 16 '25 19:12

juergen d



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!