2
select
    id,
    attempt,
    question,
    att_number,
    answer,
    timestamp,
    event
from
    mytable

MSSQL 2008 R2 で次の出力が得られます。

id,attempt,question,seq_number,answer,timestamp,event
1296040,22059,3813,0,"11960,11961,11959,11958:",1265006717,0
1296165,22059,3813,1,"11960,11961,11959,11958:11960",1265011083,2
1296166,22059,3813,1,"11960,11961,11959,11958:11960",1265011049,6
1296163,22059,3813,1,"11960,11961,11959,11958:11960",1265011037,6
1296164,22059,3813,1,"11960,11961,11959,11958:11960",1265011072,6

タイムスタンプ列の LOWEST 値を使用して、試行、質問、att_number、回答、およびイベント列の一意の行のみを選択するにはどうすればよいですか?

これ欲しい:

id,attempt,question,seq_number,answer,timestamp,event
1296040,22059,3813,0,"11960,11961,11959,11958:",1265006717,0
1296165,22059,3813,1,"11960,11961,11959,11958:11960",1265011083,2
1296163,22059,3813,1,"11960,11961,11959,11958:11960",1265011037,6
4

5 に答える 5

1

MIN で GROUP BY を使用する必要があります。

select MIN(id),
       attempt,
       question,
       seq_number,
       answer,
       MIN(timestamp),
       event
  from mytable
  GROUP BY ATTEMPT, QUESTION, SEQ_NUMBER, ANSWER, EVENT

質問からは明らかではありませんが、OP は MIN(ID) と MIN(TIMESTAMP) の両方を必要としています。また、結果例の列名は、OP の SQL のものと同じではないことに注意してください。私は、例の結果が元の SELECT ステートメントseq_numberと同じであることを受け入れることにしました。att_number

ここで SQLFiddle

共有してお楽しみください。

于 2013-07-25T10:46:53.333 に答える
1
select
id,
attempt,
question,
att_number,
answer,
timestamp,
event
from
mytable m 
where m.timestamp=(
    select min(timestamp) 
    from mytable mi 
    where mi.attempt=m.attempt and mi.question=m.question and mi.att_number=m.att_number and mi.answer=m.answer and mi.event=m.event
)
于 2013-07-25T10:49:41.830 に答える
1

group by句 を使用する必要があります。

以下selectのステートメントは、あなたの例で機能します。

select        
    min(id), attempt,question,
    att_number, answer, timestamp,
    event
from
    mytable
group by 
    attempt,question,
    att_number, answer, timestamp,
    event

しかし、本当に最低のタイムスタンプが必要な場合は、以下のように列フォームを削除する必要がありidますselect

select         
    attempt,question,
    att_number, answer, min(timestamp),
    event
from
    mytable
group by 
    attempt,question,
    att_number, answer,
    event
于 2013-07-25T10:42:55.737 に答える
-1

distinct および order by ステートメントを使用します。

于 2013-07-25T10:45:49.003 に答える