我有一个用户表
ListBoxArticle
和一个组表
id, name, points, group_id
我希望根据用户积分的总和来获得前十大组:
id, name
不幸的是,我为每个用户获得了同一组的多个条目。
答案 0 :(得分:0)
如果您希望每组//row/
SELECT
...
FROM UNNEST(XPATH(
'//row'
,XMLPARSE(DOCUMENT convert_from(pg_read_binary_file('parcel_data_first_row.xml'), 'UTF8')))
) AS myTempTable(myXmlColumn);
对于所有记录中的前10名,从
中删除[point]
[groups].[id], [groups].[name]
答案 1 :(得分:0)
我认为你走在正确的轨道上:
select top 10 g.name, g.id, SUM(u.points) as sum_points
from groups g inner join
users u
on g.id = u.group_id
group by g.name, g.id
order by sum_points desc;
注意:
points
移除group by
。SUM()
的别名与列的名称不同。这可以防止混淆,特别是在ORDER BY
。答案 2 :(得分:0)
从分组依据中删除分数并按SUM(分数)排序
select top 10 [groups].[name], [groups].[id], SUM(users.points) as points
from [groups] inner join [users] on [groups].[id] = [users].[group_id]
group by [groups].[id], [groups].[name]
order by SUM(users.points) desc