2016-12-21 7 views
0

数値が計算されるたびに処理されるリスク番号が多数あるため、毎日2つ以上のリスク番号があります。それらが計算されるたびに、実行IDが格納されます。SQLサーバーのトップランID

トップランIDのリスク番号を11月に取得するにはどうすればよいですか?

私はこのクエリを試してみました:

次の出力が得られます
select a.ptf_id,a.analysis_date, a.report_run_id, a.bps 
from rpt.rm_Report_History a 
where a.ptf_id=336 
and a.criteria_Set = 'Daily' 
and a.report_section_group = 'Key_Risk_Figures' 
and a.rm_rcp_param_name = 'Fund' 
and a.stat_class = 'standaloneVaR' 
and a.analysis_date>'2016-10-28' 

enter image description here

+0

よりも大きいデータを追加返すのだろうか? * top *を決定するものは? – GurV

+0

トップランIDは、特定のポートフォリオ(この場合はポートフォリオ)で使用可能な最新のリスク番号です。336 – Patrick

答えて

0

あなたは、各日付の値をしたい場合は、row_number()が頭に浮かぶ:

select a.* 
from (select a.ptf_id, a.analysis_date, a.report_run_id, a.bps, 
      row_number() over (partition by a.analysis_date order by a.report_run_id desc) as seqnum 
     from rpt.rm_Report_History a 
     where a.ptf_id = 336 and 
      a.criteria_Set = 'Daily' and 
      a.report_section_group = 'Key_Risk_Figures' and 
      a.rm_rcp_param_name = 'Fund' and 
      a.stat_class = 'standaloneVaR' and 
      a.analysis_date >= '2016-11-01' and 
      a.analysis_date < '2016-12-01' 
    ) a 
where seqnum = 1; 

「Novm」の月の定義方法がわかりませんber "と呼ぶ。上記は暦月を使用しています。

+0

これは、11月29日に1行を返します。 – Patrick

+0

これはあなたが望むものではありませんか? [この質問]のトップの答え(http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a) -very-simple-sql-query)には、SQL質問を投稿するためのヒントがいくつか含まれています。 –

+0

毎日、最高実行ID – Patrick

0

TRY THIS、私はまた、あなたの条件についてはかなりわからないが、あなたは与えられた基準からTOP 1run_idからレコードをしたい場合は

select a.ptf_id,a.analysis_date, a.report_run_id, a.bps 
from rpt.rm_Report_History a 
inner join (select max(run_id) as run_id 
     from rpt.rm_Report_History a 
     where a.ptf_id=336 
     and a.criteria_Set = 'Daily' 
     and a.report_section_group = 'Key_Risk_Figures' 
     and a.rm_rcp_param_name = 'Fund' 
     and a.stat_class = 'standaloneVaR' 
     and a.analysis_date>'2016-10-28') b on b.run_id = a.run_id 

あなただけにして使用して11月のデータを取得する場合between、以下のように> AND <それ以外の場合は、分析日は* *リスクの数が何であるかを定義された日付

and a.analysis_date between '2016-09-01' AND '2016-09-30' 
OR 
and a.analysis_date >= '2016-09-01' AND a.analysis_date <= '2016-09-30'