删除重复记录

时间:2009-07-15 12:27:00

标签: sql sql-server sql-server-2005 tsql

我使用以下查询

SELECT SS.sightseeingId AS 'sID'
     , SS.SightseeingName
     , SS.displayPrice AS 'Price'
     , SST.fromDate 
FROM tblSightseeings SS INNER JOIN 
     tblSightseeingTours SST ON SS.sightseeingId =  SST.sightseeingId
WHERE SS.isActive = 1 AND SS.isDisplayOnMainPage = 1 

得到这样的结果

sID | SightseeingName                        | Price | fromDate 
------------------------------------------------------------------------------
  2 | Dinner Cruise Bateaux London (Premier) |    40 | 2009-04-01 00:00:00.000
  2 | Dinner Cruise Bateaux London (Premier) |    40 | 2009-12-29 00:00:00.000
 30 | Jack The Ripper, Ghosts and Sinister   |  35.1 | 2009-04-01 00:00:00.000
 30 | Jack The Ripper, Ghosts and Sinister   |  35.1 | 2009-10-01 00:00:00.000
 40 | Grand Tour of London                   |     0 | 2009-05-01 00:00:00.000
 40 | Grand Tour of London                   |     0 | 2010-05-01 00:00:00.000
 87 | Warwick, Stratford, Oxford and The     |    25 | 2009-04-01 00:00:00.000
 87 | Warwick, Stratford, Oxford and The     |    25 | 2009-11-01 00:00:00.000

我想一次显示2次独特记录2次。重复记录归于SST.fromDate

如何更正我的查询?

4 个答案:

答案 0 :(得分:1)

嗯,记录实际上并没有重复,因为日期不同。你可以这样做:

select  SS.sightseeingId, SS.SightseeingName, SS.displayPrice,  MIN(SST.fromDate) AS FromDate 
from      tblSightseeings SS inner join 
              tblSightseeingTours SST on SS.sightseeingId =  SST.sightseeingId
where    SS.isActive = 1 and SS.isDisplayOnMainPage = 1
GROUP BY ss.sightseeingid, ss.sightseeingname, ss.displayprice

答案 1 :(得分:1)

您可以尝试下一个查询:

select  SS.sightseeingId, SS.SightseeingName, SS.displayPrice,  MAX(SST.fromDate)
from      tblSightseeings SS inner join 
              tblSightseeingTours SST on SS.sightseeingId =  SST.sightseeingId
where    SS.isActive = 1 and SS.isDisplayOnMainPage = 1
GROUP by SS.sightseeingId, SS.SightseeingName, SS.displayPrice

答案 2 :(得分:1)

试试这个(例子将返回组中的最高日期):

SELECT   SS.sightseeingId,
         SS.SightseeingName,
         SS.displayPrice,
         MAX(SST.fromDate) 
FROM     tblSightseeings SS
INNER JOIN  tblSightseeingTours SST 
         ON SS.sightseeingId = SST.sightseeingId
WHERE    SS.isActive = 1 and SS.isDisplayOnMainPage = 1
GROUP BY SS.sightseeingId,
         SS.SightseeingName,
         SS.displayPrice

根据您要显示的日期,您可以使用MAX选择最高或使用MIN选择最低。如果您有其他标准,则可能需要执行子查询。

答案 3 :(得分:-1)

仅仅排除

是不够的

SST.fromDate

来自选择?