尝试通过SQL查询从组中查找最小值

时间:2013-02-18 17:56:10

标签: sql-server sql-server-2008-r2

我非常接近这个查询(SQL Server 2008 R2):

Select selectedicao, selectedyear, selectedmonth
From weatherdaily
Where Selectedyear = 1990 and selectedmonth = 1
Group By selectedicao, selectedyear, selectedmonth

输出:

selectedicao    selectedyear    selectedmonth
-------------------------------------------------
KPKB            1990            1
KORD            1990            1
KFWA            1990            1
KCDW            1990            1
KFDY            1990            1
KLCK            1990            1

我只需要在另一个名为kfactor的列中添加Max值。

此列为每个组都有数百个值,我无法在上述查询中为每个组列出单个值。

1 个答案:

答案 0 :(得分:2)

我认为您只需要添加max()功能。

Select selectedicao, selectedyear, selectedmonth, max(kfactor) as max_kfactor
From weatherdaily
Where Selectedyear = 1990 and selectedmonth = 1
Group By selectedicao, selectedyear, selectedmonth