2013-02-18 57 views
0

我非常近有與此查詢(SQL Server 2008 R2中):試圖通過SQL查詢來找到一組最小值

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的另一列中添加最大值。

此列中每個組都有數百個值,我無法在上述查詢中爲每個組列出單個值。

+0

標題表明你想要的最小值,但細節說你想要的最大值。這是什麼? – bobs 2013-02-18 18:00:55

回答

2

我想你只需要添加max()函數。

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

是的,我認爲Group By字段必須與Select字段完全匹配,所以我從未嘗試過這種方式。謝謝 – user2084255 2013-02-18 18:22:47