您只需要使用 group by 而不是 distinct:
假设 T 是您的表:
WITH T as
(
SELECT 'Porto' City,'20.11.1988' CityDate UNION ALL
SELECT 'Porto' City,'19.11.1988' CityDate UNION ALL
SELECT 'Lisbon' City,'21.11.1988' CityDate
)
--测试1:
select City,CityDate from T GROUP BY City,CityDate Order by CityDate DESC
--结果:这仍然显示三行,因为波尔图的城市日期不一样,但如果波尔图城市日期相同,它将只显示两行。
City CityDate
Lisbon 21.11.1988
Porto 20.11.1988
Porto 19.11.1988
--测试2:
select T2.City
FROM
(select City from T GROUP BY City,CityDate) as T2
GROUP BY T2.City
或者
您可以使用 CTE:
With T as
(
select City from YourTable GROUP BY City,CityDate
)
select City FROM T group by City
- 结果:
City
Lisbon
Porto
问候