简体   繁体   English

初学者SQL-获取DISTINCT条目(按列)

[英]Beginner SQL - Get DISTINCT entries (by column)

I have a table called OnlineSpeakers that logs when (Time) people (SpeakerName) are speaking online. 我有一个名为OnlineSpeakers的表,该表记录(时间)人(SpeakerName)在网上讲话的时间。 It logs every 5 minutes the people that is currently speaking, so for example Bob would have 它每5分钟记录一次当前正在讲话的人,因此,例如Bob,

2014-06-06 07:05, Room 205, Bob
2014-06-06 07:00, Room 205, Bob
2014-06-06 06:55, Room 205, Bob
...
2014-06-06 06:00, Room 205, Bob

The table is like this: 该表是这样的:

OnlineSpeakers 在线演讲者

  • ID ID
  • Time 时间
  • RoomName 房间名称
  • SpeakerName 演讲者姓名

I would like to extract a list of the newest speakers that has been registered. 我想提取一个已注册的最新发言人的名单。 So for Bob it would be 'Bob, 2014-06-06 06:00' that would signal Bob's debut as a speaker. 因此,对于鲍勃来说,这是“鲍勃,2014-06-06 06:00”,这标志着鲍勃首次登台演讲。

I know how to get this for Bob: 我知道如何为鲍勃买到这个:

SELECT TOP (1) Time, SpeakerName
FROM     OnlineSpeakers
WHERE  (SpeakerName = 'Bob')
ORDER BY Time ASC

But I don't know how to get it for each speaker in OnlineSpeakers 但是我不知道如何为OnlineSpeakers中的每个发言人获取它

SELECT DISTINCT SpeakerName
FROM     OnlineSpeakers

I would like a list like this: 我想要一个这样的清单:

2014-06-06 06:00, Room 205, Bob
2014-06-06 05:00, Room 205, Tim
2014-06-06 03:55, Room 205, George
2014-06-06 06:00, Room 205, Martin
...

I tried replacing the "SpeakerName = 'Bob'" "with a SpeakerName IN" and then a list of unique names, but then it just gives the full list of the table with multiple time for each speaker. 我尝试用“ SpeakerName IN”替换“ SpeakerName ='Bob'”“,然后替换为唯一名称列表,但随后它仅给出了该表的完整列表,每个扬声器都有多个时间。

Any help is appreciated. 任何帮助表示赞赏。

You need the Group By operator. 您需要分组依据运算符。

select SpeakerName, Min(Time) from OnlineSpeakers
Group by SpeakerName

This will take all your and group them by the name of the speaker, when you do this you will need to find a way to decide which of the range of Times you want to display (this is called an aggregate function). 这将把您的所有内容都按演讲者的姓名分组,当您执行此操作时,您将需要找到一种方法来决定要显示的时间范围中的哪一个(这称为汇总函数)。 However you've already told us you want the first, therefore you can use the Min function. 但是,您已经告诉我们您想要第一个,因此可以使用Min函数。

Bring me all the OnlineSpeakers , grouped by the SpeakerName and include the first Time with each group. 给我所有的OnlineSpeakers ,由分组SpeakerName ,包括与各组的第一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM