2009-07-15 175 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

從選擇