如何查询最近n个月的最大金额和核心申请日期

时间:2017-12-11 06:48:17

标签: sql-server-2008

给出像'2017-12-10'这样的日期 我知道如何使用相应的日期查询上个月的最小金额:

Begin
  declare a_dt date
  declare dt1 date
  declare dt2 date

  select 
select  amt, valuation_dt, 
 'min in the mth' 'note'  from dba.summary_by_fin_grp
 where fi_group_cd='RBCFG'
and valuation_dt between '2017-11-01' and '2017-11-30 23:59:59.999'
and net_ValueWOBankAcct = 
(select  min(net_ValueWOBankAcct) from dba.summary_by_fin_grp
  where fi_group_cd='RBCFG'
 and valuation_dt between '2017-11-01' and '2017-11-30 23:59:59.999')

我也知道我可以用

循环
while expr 
begin
    ...
end

现在如何在没有使用c#

的帮助进行硬编码的情况下,以最后几个月的每月的日期返回最小值

请注意我最终还要列出最大值

2 个答案:

答案 0 :(得分:0)

不要这样做:

AND valuation_dt BETWEEN '2017-11-01' AND '2017-11-30 23:59:59.999'

这更简单,更准确无论数据中有多少时间适用:

AND valuation_dt >='2017-11-01' AND valuation_dt < '2017-12-01'

请注意它实际上是如何缩短的:)

至少,使用&#34;分析&#34;该函数的形式MIN() OVER

SELECT
      amt
    , valuation_dt
    , min_in_month
    , 'note'
FROM (
      SELECT
            amt
          , valuation_dt
          , net_ValueWOBankAcct
          , MIN(net_ValueWOBankAcct) OVER (PARTITION BY fi_group_cd) AS min_in_month
          , 'note'
      FROM dba.summary_by_fin_grp
      WHERE fi_group_cd = 'RBCFG'
      AND valuation_dt >= '2017-11-01' AND valuation_dt < '2017-12-01'
      ) d
WHERE net_ValueWOBankAcct = min_in_month

您还可以使用以下内容,以获得每月最低费用

SELECT
      amt
    , valuation_dt
    , min_in_month
    , 'note'
FROM (
      SELECT
            amt
          , valuation_dt
          , net_ValueWOBankAcct
          , MIN(net_ValueWOBankAcct) OVER (PARTITION BY fi_group_cd, year(valuation_dt), month(valuation_dt)) AS min_in_month
          , 'note'
      FROM dba.summary_by_fin_grp
      WHERE fi_group_cd = 'RBCFG'
      AND valuation_dt >= '2017-08-01' AND valuation_dt < '2017-12-01'
      ) d
WHERE net_ValueWOBankAcct = min_in_month

注意他&#34;分区&#34;的变化现在包括年月和月份

答案 1 :(得分:0)

不确定这是否是最好的方法,但我从下面得到了我需要的格式的结果。唯一的错误是,如果min_in_month列或max_in_month不是唯一的,则可能会在给定月份重复输入 我知道used_by_已经说过之间没有使用但是之间是定义包含和从sql 2008和许多其他主流数据库工作

Begin
declare n int;
declare aDt date;
declare dt1 date;
declare dt2 date;
declare nullDt date;
declare nullBal money;

select 7, today(), null,null into n, aDt, nullDt, nullBal;
// no EOFMonth in sql 2008
select dateAdd(month, -n-1, aDt - Day(aDt)+1) into dt1;
select aDt - Day(aDt) into dt2;

SELECT
      Latest_Bal
    , min_Dt
    , min_in_month
    , max_Dt  // Need to get this value somehow?
    , max_in_month
    , YearMth
 into #temp
  FROM (        
      SELECT
            null as Latest_Bal
          , nullDt as max_Dt
          , valuation_dt as min_Dt
          , net_ValueWOBankAcct
          , datepart(Year, valuation_dt)*100+datepart(mm, valuation_dt) as YearMth
          , MIN(net_ValueWOBankAcct) OVER (PARTITION BY YearMth) AS min_in_month
          , MAX(net_ValueWOBankAcct) OVER (PARTITION BY YearMth) AS MAX_in_month

      FROM dba.pf_dailyvaluation_summary_by_fin_grp
      WHERE fi_group_cd = 'RBCFG'
      AND valuation_dt between dt1 and dt2
      ) d

   WHERE net_ValueWOBankAcct = min_in_month --or net_ValueWOBankAcct = max_in_month 
    ;

update #temp t
set t.max_dt=(select first valuation_dt 
 from dba.pf_dailyvaluation_summary_by_fin_grp f
  where t.YearMth = year(f.valuation_dt)*100+month(f.valuation_dt)
     and net_ValueWOBankAcct= t.MAX_in_month )
;

select * from #temp;
drop table #temp;
end