私は上記の画像を照会したいと思います。
左の画像は元のデータ、右の画像はクエリデータです。
select distinct ID, Nickname, Revision
from test_table
このクエリは上記の画像には表示されません。
データの重複を避けるにはどうすればよいですか?
私は上記の画像を照会したいと思います。
左の画像は元のデータ、右の画像はクエリデータです。
select distinct ID, Nickname, Revision
from test_table
このクエリは上記の画像には表示されません。
データの重複を避けるにはどうすればよいですか?
SQL Serverの、サブクエリで窓関数ROW_NUMBER
を使用している場合:
select t.id, t.nickname, t.revision
from (
select t.*, row_number() over (
partition by t.id order by t.revision desc
) rn
from your_table t
) t
where rn = 1;
それともTOP with ties
ROW_NUMBER
を使用して:
select top 1 with ties *
from your_table
order by row_number() over (
partition by id order by revision desc
)
MySQLの場合:
select t.*
from your_table t
inner join (
select id, MAX(revision) revision
from your_table
group by id
) t1 on t.id = t1.id
and t.revision = t1.revision;
もう一つのトリックはTOP 1 with TIES
SELECT Top 1 with ties *
FROM your_table t
Order by row_number() over (partition BY t.id order by t.revision DESC)
select distinct ID, Nickname, MAX(Revision)
from test_table
group by ID
同じ文でdistinctとgroup by句の両方を使用する必要はありません –
あなたは現在どのような結果を得ていますか? –
最後のリビジョンデータのみを取得したい。私はSQL Serverを使用しています。 – somputer
@JaydipJ - BobTは、ユーザーID 1の3番目のバージョンです。 – Hogan