で上位10種類の(別のフィールドを)希望:各カテゴリについては各カテゴリ(分野)のために、私はのは、私はこのようになりますテーブルがあるとしましょう、カウント
+------------+-------------+-------+
| Category | Type | Count |
+------------+-------------+-------+
| Fruits | Apple | 13 |
| Vegetables | Carrot | 7 |
| Legumes | Kidney Bean | 1 |
| Fruits | Orange | 1 |
| Vegetables | Green | 3 |
| Legumes | Black Bean | 1 |
| Vegetables | Leek | 1 |
| Fruits | Banana | 1 |
| Legumes | Lentil | 1 |
| Fruits | Mango | 1 |
| Fruits | Pinapple | 18 |
| Fruits | Strawberry | 1 |
| Legumes | Flat Bean | 2 |
| Vegetables | Brocolli | 8 |
| Fruits | Rambotan | 1 |
| Fruits | Marang | 15 |
| Vegetables | Cauliflower | 5 |
| Vegetables | Aubergine | 1 |
+------------+-------------+-------+
、私は希望しますカウントで上位10種類。
問題のテーブルが実際には何百万行もある場合、私が単にselect category, type, sum(Count) group by category, type order by category, type
を実行した場合、そのタイプがトップ10にない結果が得られます。
私はpostgresqlを使用していますが、これを行うための「一般的な」SQLの方法があると思われます。ある?
は
チェックこれをDENSE_RANKすることができます - https://spin.atomicobject.com/2016/03/12/select-top-n-per-group-postgresql/ – tom