0

以下のようなテーブルがあり、行 2、9、11、20 などの異なる人を選択したいと考えています。MAX() はランダムではないため、選択したくありません。また、Jack を 2 回選択したくありません。レコードの各セットから 1 人である必要があります

ID  Name    Category Level
1   Jack    Dragon     3
2   Jack    Falls      5
3   Jack    Spider     5
4   Jack    Apprentice 1
5   Jack    Jolly      5
6   Luke    Dragon     1
7   Luke    Falls      1
8   Luke    Spider     3
9   Luke    Apprentice 5
10  Luke    Jolly      5
11  Mark    Dragon     3
12  Mark    Falls      3
13  Mark    Spider     1
14  Mark    Apprentice 3
15  Mark    Jolly      1
16  Sam     Dragon     3
17  Sam     Falls      5
18  Sam     Spider     5
19  Sam    Apprentice  5
20  Sam    Jolly       3
4

3 に答える 3

0

これは思ったよりもトリッキーに思えますが、列をtemp追加してテーブルを作成するとうまくいくはずです。tempId試す:

create table #temp(ID int, Name char(10), Category char(10), Level int, tempId varchar(36))

insert #temp select ID, Name, Category, Level, NEWID() as 'tempId' from yourTable
select ID, Name, Category, Level from #temp where ID IN
    (select min(tempId) from #temp group by Name)

drop table #temp
于 2016-03-10T18:35:02.247 に答える
0
SELECT DISTINCT name
from tbl
ORDER BY NEWID()
于 2016-03-10T16:56:53.420 に答える