oracle 当前时间下,周、日、月的交易统计

日:

SELECT  COUNT(1)
		FROM STUDENT_BILL  WHERE 1=1
		
			AND SUB_MERCHANT_ID = #{merchantId}
		
		
			AND to_char(PAY_TIME,'yyyy-mm-dd')= to_char(to_date(#{date}, 'yyyy-mm-dd hh24:mi:ss')-#{type},'yyyy-mm-dd') AND STATUS = '1'
		
		
			AND to_char(PAY_TIME,'yyyy-mm-dd')= to_char(to_date(#{date}, 'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd') AND STATUS = '1'
		

 

周:

SELECT  COUNT(1)
		FROM STUDENT_BILL  WHERE 1=1
		
			AND SUB_MERCHANT_ID = #{merchantId}
		
		
			AND PAY_TIME between trunc(next_day(to_date(#{date}, 'yyyy-mm-dd hh24:mi:ss') - 1, 1)-6) AND  trunc(next_day(to_date(#{date}, 'yyyy-mm-dd hh24:mi:ss') - 1, 1)+1) AND STATUS = '1'
		
		
			AND PAY_TIME between trunc(next_day(to_date(#{date}, 'yyyy-mm-dd hh24:mi:ss') -(1+#{type}*7), 1)-6) AND  trunc(next_day(to_date(#{date}, 'yyyy-mm-dd hh24:mi:ss') - (1+#{type}*7), 1)+1) AND STATUS = '1'
		

月:

SELECT  COUNT(1)
		FROM STUDENT_BILL  WHERE 1=1
		
			AND SUB_MERCHANT_ID = #{merchantId}
		
		
			AND to_char(PAY_TIME,'yyyy-mm')= to_char(add_months(to_date(#{date}, 'yyyy-mm-dd hh24:mi:ss'),0),'yyyy-mm') AND STATUS = '1'
		
		
			AND to_char(PAY_TIME,'yyyy-mm')= to_char(add_months(to_date(#{date}, 'yyyy-mm-dd hh24:mi:ss'),-#{type}),'yyyy-mm') AND STATUS = '1'
		

备注:type的值 :0表示当日,1表示昨天,依次往前,想获取几天一次增加type的值

           周和月一样,0表示当月当周,1表示上一月上一周等

           周的时间是:周一到周日的交易区间

日期具体获取方式:

//周

select  next_day(to_date('2018-8-3','yyyy-MM-dd'),1) from dual;//找到下一周的第一天,也就是日历中的下一周星期日
select  next_day(to_date('2018-8-3','yyyy-MM-dd')-8,1) from dual;//找到本周的周日,本周的第一天
select  trunc(next_day(to_date('2018-8-3','yyyy-MM-dd')-1,1)-6) from dual;//找到本周的星期一,本周的第二天
select  trunc(next_day(to_date('2018-8-3','yyyy-MM-dd')-1,1)+1) from dual;//找到下一周的星期一,下周的第二天
select  trunc(next_day(to_date('2018-8-3','yyyy-MM-dd')-8,1)-6) from dual;//找到上周的星期一,上周的第二天
//所以区间为本周一的00:00:00到下周一的00:00:00,而不是到下周天的23:59:59

//日

select  to_char(to_date('2018-8-3', 'yyyy-mm-dd hh24:mi:ss')-3,'yyyy-mm-dd') from dual;//获取当前日期减去几就是从今天起往前推几天

//月

select  to_char(add_months(to_date('2018-8-3', 'yyyy-mm-dd hh24:mi:ss'),-2),'yyyy-mm') from dual;//获取当前月,再传入负几就是从这月起往前推几月

 

你可能感兴趣的:(MySQL)