【问题标题】:Filter by financial period Sql按财务期筛选 Sql
【发布时间】:2015-12-11 13:39:15
【问题描述】:

我正在尝试收集当前财政年度的所有数据。 请在下面查看我的代码。

       select *

   FROM            dbo.vw_AN_Admin_VendorReturns

       where 
           case  month(getdate()) <4  -- If current month is less than April (old financial year)
               year(RtnDt) = year(getdate()) and month(RtnDt) <4 or year(RtnDt) = year(getdate())-1 and month(RtnDt) >3 

            case  month(getdate()) >3 -- If current month is more than March (New financial year)
                year(RtnDt) = year(getdate()) and month(RtnDt) >3 or year(RtnDt) = year(getdate())+1 and month(RtnDt) <4
            end

【问题讨论】:

  • 无法理解你的问题..
  • 解释一下你想做什么???
  • 请发布示例数据和预期结果/
  • @Praveen 我如何发布数据?
  • @Krishn 在问题中包含数据或提供 sqlfiddle 链接

标签: sql tsql date case


【解决方案1】:

Krishn,根据您的简短描述,这应该可以解决您的问题。

select * 
from dbo.vw_AN_Admin_VendorReturns
Where 
Case 
    When Month(getdate())<4 
        Then Year(getdate())-1 
    Else 
        Year(getdate()) 
End 
= 
Case 
    When Month(RtnDt)<4 
        Then Year(RtnDt)-1 
    Else Year(RtnDt) 
End 

【讨论】:

    【解决方案2】:

    据我了解,您可能正在搜索这些公式。

    declare @toDate datetime = dateadd(day, -day(GETDATE()), GETDATE())
    declare @fromDate datetime = dateadd(year, -1, @toDate)
    

    只需将当前日期减去天数,就可以得到上个月的最后一天,然后将该日期减去一年,得到 1 年的期限。

    select 
      D.* 
    from mydata D
    where D.RtnDt > @fromDate and D.RtnDt <= @toDate
    

    您不必声明变量,只是为了更好地阅读。 希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-10-15
      • 2010-09-28
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多