select语句根据rownumber选择distinct

时间:2011-04-07 17:30:18

标签: sql sql-server-2005

我有一个非常简单的查询,但有些预订列重复,如果他们这样做我不想显示它们,但是整个行不一样我怎么能这样做如果行是重复显示一个最大总数。

SELECT     PREMIUMYEAR, 
           LASTNAME, 
           FIRSTNAME, 
           BOOKING, 
           CLAIMTYPE, 
           DEPARTUREDATE, 
           [PLAN], 
           INCIDENT, 
           RESERVEAMOUNT, 
           FINALSTATUS, 
           AGE, 
           TOTAL
FROM       dbo.[table]

1 个答案:

答案 0 :(得分:4)

SELECT PREMIUMYEAR, LASTNAME, FIRSTNAME, BOOKING, CLAIMTYPE, DEPARTUREDATE, [PLAN], INCIDENT, RESERVEAMOUNT, FINALSTATUS, AGE, TOTAL
    FROM (SELECT PREMIUMYEAR, LASTNAME, FIRSTNAME, BOOKING, CLAIMTYPE, DEPARTUREDATE, [PLAN], INCIDENT, RESERVEAMOUNT, FINALSTATUS, AGE, TOTAL,
                 ROW_NUMBER() OVER(PARTITION BY BOOKING ORDER BY TOTAL DESC) AS RowNum
              FROM dbo.[table]) t
    WHERE t.RowNum = 1