简体   繁体   English

SQL Server 2008选择

[英]SQL Server 2008 SELECT

I have a table with lots of columns. 我的桌子上有很多列。 One of the columns is AppointmentNo , and this column can be either 1 or 2 (basically it is either a first appointment or followup). 列之一是AppointmentNo ,此列可以是1或2(基本上是第一次约会或随访)。

Some of the columns include 一些专栏包括

tblAppoints : tblApoints

ClientID
ClientFirstName
ClientLastName
ClientAddress
ClientAppointmentNo

I'm trying to select clientID 's from this table, however, I don't want to see any clients where the ClientAppointmentNo = 2 . 我正在尝试从此表中选择clientID ,但是,我不希望看到ClientAppointmentNo = 2任何客户端。 So only show clients that have AppointmentNo = 1 , no clients with ClientAppointmentNo = 2 . 因此,仅显示具有AppointmentNo = 1客户端,不显示具有ClientAppointmentNo = 2客户端。

Here is one method, using aggregation: 这是一种使用聚合的方法:

select a.clientId
from tblAppoints a
group by a.clientId
having max(ClientAppointmentNo) = 1;

If you want to see the appointment details, then one method uses window functions: 如果要查看约会详细信息,则一种方法使用窗口函数:

select a.*
from (select a.*,
             max(ClientAppointmentNo) over (partition by a.clientId) as maxcan
      from tblAppoints a
    ) a
where maxcan = 1;

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

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