0

I have a table of data like this:

id  user_id  A  B  C
=====================
1   15       1  2  3
2   15       1  2  5
3   20       1  3  9
4   20       1  3  7

I need to remove duplicate user ids and keep the record that sorts lowest when sorting by A then B then C. So using the above table, I set up a temp query (qry_temp) that simply does the sort--first on user_id, then on A, then on B, then on C. It returns the following:

id user_id  A  B  C
====================
1  15       1  2  3
2  15       1  2  5
4  20       1  3  7
3  20       1  3  9

Then I wrote a Totals Query based on qry_temp that just had user_id (Group By) and then id (First), and I assumed this would return the following:

user_id  id
===========
15       1
20       4

But it doesn't seem to do that--instead it appears to be just returning the lowest id in a group of duplicate user ids (so I get 1 and 3 instead of 1 and 4). Shouldn't the Totals query use the order of the query it's based upon? Is there a property setting in the query that might impact this or another way to get what I need? If it helps, here is the SQL:

SELECT qry_temp.user_id, First(qry_temp.ID) AS FirstOfID
FROM qry_temp
GROUP BY qry_temp.user_id;
4

1 回答 1

2

您需要不同类型的查询,例如:

SELECT tmp.id,
       tmp.user_id,
       tmp.a,
       tmp.b,
       tmp.c
FROM   tmp
WHERE  (( ( tmp.id ) IN (SELECT TOP 1 id
                         FROM   tmp t
                         WHERE  t.user_id = tmp.user_id
                         ORDER  BY t.a,
                                   t.b,
                                   t.c,
                                   t.id) )); 

其中 tmp 是您的表的名称。First、Last、Min 和 Max 不依赖于排序顺序。在关系数据库中,排序顺序非常短暂。

于 2012-11-19T15:10:10.540 回答