我有一个类似...的方桌
id | pname | ptype | amount
------------------------------------------------------
1 | aaaaa | qw | 20
2 | bbbbb | qw | 25
3 | ccccc | qw | 55
4 | aaaaa | qw | 42
我想得到像
id | pname | ptype | amount
------------------------------------------------------
1 | aaaaa | qw | 42
2 | bbbbb | qw | 25
3 | ccccc | qw | 55
表示最大数量的非重复pname ...
我应该运行哪个查询?
最佳答案
在SQLite 3.7.11或更高版本(Android API级别16或更高版本)中,您可以简单地在SELECT列表中使用MAX,其他值将保证来自具有最大值的记录:
SELECT id,
pname,
ptype,
MAX(amount) AS amount
FROM MyTable
GROUP BY pname
如果需要使用早期版本,则需要明确检查:
SELECT *
FROM MyTable
WHERE NOT EXISTS (SELECT 1
FROM MyTable AS smaller
WHERE smaller.pname = MyTable.pname
AND smaller.amount < MyTable.amount)