select category, count, count/total percent
from
(
select category, count(category) count
from tweets
group by category
) c JOIN (
select count(*) total
from tweets
) t
に出力にそれを行うための一つの方法である:
+----------+-------+---------+
| category | count | percent |
+----------+-------+---------+
| negative | 1 | 0.3333 |
| neutral | 1 | 0.3333 |
| positive | 1 | 0.3333 |
+----------+-------+---------+
を...それは可能であろうか? oは0.3333ではなく33%だけを返しますか?
select category, count, round(count/total * 100) percent
from
(
select category, count(category) count
from tweets
group by category
) c JOIN (
select count(*) total
from tweets
) t
+----------+-------+---------+
| category | count | percent |
+----------+-------+---------+
| negative | 1 | 33 |
| neutral | 1 | 33 |
| positive | 1 | 33 |
+----------+-------+---------+
あなたが%
を追加したい場合は、concat(round(count/total * 100), '%')
を行う使用することができますが、私は強くクライアントコードに(書式設定の任意の並べ替えを)それを実行することをお勧め。あなたの現在のクエリに
回答ありがとうございました@peterm 0.3333ではなく33%しか返すことはできませんか? –
これは素晴らしいですね。@peterm –