2017-07-06 24 views
1

MS-SQL 2012を使用する。大規模な気象データセットから特定のデータフィールドを取得しようとすると、本当に困惑します。月と曜日の値を月ごとにグループ化して返す

私が正しく参照するために、それが発生した日時/月の値と一緒に、毎日の最大値を引き戻す#max_tempと呼ばれる一時テーブルまで、この大規模な生データファイル取り除かれている:

monthid month day time current_temp 
1  12  24 12:45 9.1 
1  12  25 12:25 8.3 
1  12  26 23:55 8.6 
1  12  27 00:00 8.6 
1  12  28 13:15 5.9 
1  12  29 12:50 5 
1  12  30 13:32 6.3 
1  12  31 12:49 6.9 
2  1  1 23:59 12 
2  1  2 01:12 12.7 
2  1  3 03:55 6.2 
を私は次のコードが、最終溶液になったり、クエリが失敗しない試してみました

monthid  month day time current_temp 
    1  12  24 12:45 9.1 
    2  1  9 20:04 15.1 <<*not shown in above sample*>> 

他の同様の質問を見てから:

私は何を取得したいのはそう返すmonthIDによってグループ化された出力は、です。

select * 
from (select t.*, ROW_NUMBER() over (partition by t.monthid, t.time order by t.current_temp desc) as rn 
from #max_temp t) x 
where rn=1 
order by monthid asc 

または

select monthid, day, time, current_temp 
from #max_temp 
where current_temp= (select max(current_temp) from #max_temp group by MonthID, day, time) 

あなたの助け、 エリオットを事前に感謝します。

+0

@SqlZimがこれに答えた場合は、返信としてマークしてください。 –

答えて

3

ので、同じようpartition byからt.timeを削除します、

select * 
from (
    select t.*, ROW_NUMBER() over (partition by t.monthid order by t.current_temp desc) as rn 
    from #max_temp t 
) x 
where rn=1 
order by monthid asc 

は、パーティション内のtimeを持つことは、あなたにそれぞれmonthidtimeためcurrent_tempための最大値を与えるだろうが、あなただけの各monthidの最大のcurrent_tempをしたいので、その式からtimeを削除します。

+0

ありがとう、それは、私はそれが近いと思った....! –

+0

@ElliotWorthうん、とても近い!お力になれて、嬉しいです – SqlZim

関連する問題