7

重複および三重の行を含むテーブルがあります - 重複しているが最新のタイムスタンプを持つ行と重複していない行を選択するにはどうすればよいですか?

 -------------------------------------
| pk_id | user_id | some_timestamp    |
|-------------------------------------|
| 1     | 123     | 10-Jun-12 14.30   |
| 2     | 123     | 19-Jun-12 21.50   |
| 3     | 567     | 10-Jun-12 09.23   |
| 4     | 567     | 12-Jun-12 09.45   |
| 5     | 567     | 13-Jun-12 08.40   |
| 6     | 890     | 13-Jun-12 08.44   |
 -------------------------------------

私が終わるように:

 -------------------------------------
| pk_id | user_id | some_timestamp    |
|-------------------------------------|
| 2     | 123     | 19-Jun-12 21.50   |
| 5     | 567     | 13-Jun-12 08.40   |
| 6     | 890     | 13-Jun-12 08.44   |
 -------------------------------------
4

5 に答える 5

8
SELECT * FROM ( 
SELECT pk_id, 
       user_id, 
       some_timestamp, 
       ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY some_timestamp DESC) col 
FROM table) x  
WHERE x.col = 1
于 2012-12-31T10:01:38.837 に答える
4

これを試して

select * from table 
where some_timestamp 
   in (select max(some_timestamp)
       from table group by user_id)
于 2012-12-31T09:44:43.400 に答える
2

これを試してください。正しいデータセットを返す SQLFIDDLE を作成しました

SELECT * FROM YourTable AS T1
INNER JOIN
( SELECT user_id , MAX(some_timestamp) AS some_timestamp FROM YourTable 
GROUP BY user_id
) AS T2
ON T1.User_Id = T2.User_Id AND T1.some_timestamp = T2.some_timestamp
ORDER BY 1

http://sqlfiddle.com/#!6/f7bba/6

于 2012-12-31T10:01:26.533 に答える
1
select YourTable.*
from
  YourTable JOIN
  (select User_Id, Max(Some_Timestamp) as Mx
   from YourTable
   group by User_Id) Mx
  on YourTable.User_Id=Mx.User_Id
   and YourTable.Some_Timestamp=Mx.Mx
于 2012-12-31T09:56:40.043 に答える
1

これを試して:

select * from my_table
where (user_id, some_timestamp) IN (select user_id, max(some_timestamp) from my_table group by user_id);
于 2012-12-31T09:59:33.133 に答える