选择多个相同记录的第一行

时间:2013-05-30 20:18:21

标签: sql-server-2008

我正在使用的查询有多个名字,一个电子邮件地址的姓氏。我如何只选择第一行并忽略剩余。 像

        Tracy     Dugan  td@test.com
        stephen   stacy  td@test.com
        Ron       Thomas td@test.com

结果Tracy Dugan td@test.com 我有多行具有相同的电子邮件地址。如何实现

      SELECT distinct f.email_address,TITLE,FIRST_NAME,LAST_NAME FROM
      #tempFinal F,temp htc
      where f.EMAIL_ADDRESS = htc.EMAIL_ADDRESS 

1 个答案:

答案 0 :(得分:1)

email_address对结果集进行分区,并使用row_number分配序列,然后过滤。使用order by子句

调整序列条件
select firstname, lastname, email_address from (
select firstname, lastname, email_address, row_number() over
(partition by email_address order by firstname, lastname) as ranker
from table ) Z where ranker = 1