2009-07-15 188 views
2

我使用下面的查询删除重复记录

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一次30一次40一次。重复记录归因于SST.fromDate

如何纠正我的查询?

+0

你想显示哪个日期? – Josh 2009-07-15 12:29:04

+0

最近日期 – Waheed 2009-07-15 12:47:03

回答

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 
+0

或您的面包车使用MIN(SST.fromDate)...这取决于您想要查看的日期。 – 2009-07-15 12:31:03

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

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

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的最低值来选择最高。如果你有其他标准,你可能需要做一个子查询。

-1

岂不是不够的,只是排除

SST.fromDate

从选择