sql 如何计算财务季度结束前的剩余天数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 如何计算财务季度结束前的剩余天数相关的知识,希望对你有一定的参考价值。


----------------------------------------Vertica----------------------------------------------------------------------------------

---calculate remaining days in fiscal quarter
select
case 
	when Month(Now()) in (11,12,1) then 'FQ1'
	when Month(Now()) in (2,3,4) then 'FQ2'
	when Month(Now()) in (5,6,7) then 'FQ3'
	when Month(Now()) in (8,9,10) then 'FQ4'
end as CurrentFiscalQuarter,

case 
	when Month(Now()) in (11,12,1) Then datediff('dd', Date(Now()), to_date(Concat(Year(Now()),'-01-31'), 'YYYY-MM-DD')) 
	when Month(Now()) in (2,3,4) Then datediff('dd', Date(Now()), to_date(Concat(Year(Now()),'-04-30'), 'YYYY-MM-DD')) 
	when Month(Now()) in (5,6,7) Then datediff('dd', Date(Now()), to_date(Concat(Year(Now()),'-07-31'), 'YYYY-MM-DD')) 
	when Month(Now()) in (8,9,10) Then datediff('dd', Date(Now()), to_date(Concat(Year(Now()),'-10-31'), 'YYYY-MM-DD')) 
end as RemainingDaysInQuarter


--------------------------------SQL Server--------------------------------------------------------------------------------------

select

case 
	when Month(getdate()) in (11,12,1) then 'FQ1'
	when Month(getdate()) in (2,3,4) then 'FQ2'
	when Month(getdate()) in (5,6,7) then 'FQ3'
	when Month(getdate()) in (8,9,10) then 'FQ4'
end as CurrentFiscalQuarter,

case 
	when Month(getdate()) in (11,12,1)
		then datediff(day, getdate(), convert(date, Concat(Year(getdate())+1,'-01-31')) ) 
		
	when Month(getdate()) in (2,3,4)
		then datediff(day, getdate(), convert(date, Concat(Year(getdate()),'-04-30')) ) 

	when Month(getdate()) in (5,6,7)
		then datediff(day, getdate(), convert(date, Concat(Year(getdate()),'-07-31')) ) 

	when Month(getdate()) in (8,9,10)	
		then datediff(day, getdate(), convert(date, Concat(Year(getdate()),'-10-31')) )
		

--------------------------------MySQL--------------------------------------------------------------------------------------
		
select		

case 
	when Month(Now()) in (11,12,1) then 'FQ1'
	when Month(Now()) in (2,3,4) then 'FQ2'
	when Month(Now()) in (5,6,7) then 'FQ3'
	when Month(Now()) in (8,9,10) then 'FQ4'
end as CurrentFiscalQuarter,

case 
	when Month(Now()) in (11,12,1)
		then timestampdiff(day, Now(), str_to_date(Concat(Year(Now())+1,'-01-31'), '%Y-%m-%d') ) 
		
	when Month(Now()) in (2,3,4)
		then timestampdiff(day, Now(), str_to_date(Concat(Year(Now()),'-04-30'), '%Y-%m-%d') ) 

	when Month(Now()) in (5,6,7)
		then timestampdiff(day, Now(), str_to_date(Concat(Year(Now()),'-07-31'), '%Y-%m-%d') ) 

	when Month(Now()) in (8,9,10)	
		then timestampdiff(day, Now(), str_to_date(Concat(Year(Now()),'-10-31'), '%Y-%m-%d') )
		
end as Remaining_in_Fis_Quarter
 
 
 --------------------------------PostgreSQL--------------------------------------------------------------------------------------
 
 select

 case 
	when date_part('month', Now()) in (11,12,1) then 'FQ1'
	when date_part('month', Now()) in (2,3,4) then 'FQ2'
	when date_part('month', Now()) in (5,6,7) then 'FQ3'
	when date_part('month', Now()) in (8,9,10) then 'FQ4'
end as CurrentFiscalQuarter,

case 
	when date_part('month', Now()) in (11,12,1)
		then date_part('day', to_date(date_part('year', current_date)+1 || '-01-31', 'YYYY-MM-DD') - Now() ) + 1
	
	when date_part('month', Now()) in (2,3,4)
		then date_part('day', to_date(date_part('year', current_date) || '-04-30', 'YYYY-MM-DD') - NoW() ) + 1 
		
	when date_part('month', Now()) in (5,6,7) 
		then date_part('day', to_date(date_part('year', current_date) || '-07-31', 'YYYY-MM-DD') - Now() ) + 1

	when date_part('month', Now()) in (8,9,10)	
		then date_part('day', to_date(date_part('year', current_date) || '-10-31', 'YYYY-MM-DD') - Now() ) + 1
		
end as Remaining_In_Fis_Quarter

以上是关于sql 如何计算财务季度结束前的剩余天数的主要内容,如果未能解决你的问题,请参考以下文章

如何计算给定日期的剩余天数、小时数、分钟数和秒数?

计算剩余天数[重复]

计算员工在开始和结束日期之间应报告工作的天数

使用 javascript 获取当前季度

如何在 Oracle SQL 中滚动计算每个用户的天数?

用sql语句求本季度比上一个季度多多少天