这是有问题的表格,它是根据这个问题底部的命令生成的:
+--------------+-----------------+
| usr_postcode | pdp_point_total |
+--------------+-----------------+
| MS2 8LA | 160 |
| MS2 8LA | 140 |
| MS2 8LA | 110 |
| MS2 8LA | 100 |
| MS2 8LA | 90 |
| MS2 8LA | 80 |
| MS2 8LA | 50 |
| MS2 8LA | 30 |
| WN4 9NV | 25 |
| MS2 8LA | 20 |
| SL6 1SR | 10 |
| SL1 4DX | 10 |
+--------------+-----------------+
我想在第一列中为每个值找到第二列中的最大值,因此输出将类似于:
-------------
MS2 8LA | 160
WN4 9NV | 25
SL6 1SR | 10
SL1 4DX | 10
-------------
这两列来自不同的表,所以目前这是我的命令:
SELECT DISTINCT users.usr_postcode, point_date_pairs.pdp_point_total FROM users
INNER JOIN point_date_pairs ON users.usr_id_pk = point_date_pairs.usr_id_fk
ORDER BY point_date_pairs.pdp_point_total DESC;
答案 0 :(得分:1)
GROUP BY应该有所帮助:
SELECT users.usr_postcode, MAX(point_date_pairs.pdp_point_total)
FROM users
INNER JOIN point_date_pairs ON users.usr_id_pk = point_date_pairs.usr_id_fk
GROUP BY users.usr_postcode ASC