我一直在浏览论坛,试图找到解决问题的方法。
我在SQL Server 2000中有一个名为result
的表,其中包含以下数据;
City Price DepDate RetDate
Barcelona 145 2010-05-15 2010-05-20
New York 400 2010-06-20 2010-06-20
Barcelona 160 2010-05-17 2010-05-22
New York 325 2010-05-10 2010-05-18
Istanbul 250 2010-06-22 2010-06-27
我想在SQL Server 2000中查询,以便返回所有列,但它在城市上是不同的/唯一的,只显示最低价格。
所以我希望它返回以下
Barcelona 145 2010-05-15 2010-05-20
New York 325 2010-05-10 2010-05-18
Istanbu 250 2010-06-22 2010-06-27
我不知道如何做到这一点,我应该很容易,而且我确信在提出解决方案时我会感到愚蠢。
有谁知道怎么做?
Brgds, 埃里克
答案 0 :(得分:1)
SELECT
A1.*
FROM
[TableName] A1
INNER JOIN
(
SELECT
A.City
, A.Price
, MIN(A.DepDate) AS DepDateMin
FROM
[TableName] A
INNER JOIN
( SELECT
City
, MIN(Price) AS PriceMin
FROM
[TableName]
GROUP BY City
) B ON A.City = B.City AND A.Price = B.PriceMin
GROUP BY
A.City, A.Price
) B1 ON A1.City = B1.City AND A1.Price = B1.PriceMin AND A1.DepDate = B2.DepDateMin
答案 1 :(得分:0)
尝试这个
select City,Price,DepDate,,RetDate from Your_table a
where a.Price = (select min(Price) from Your_table b
where a.city = b.city group by city)