HK学习笔记
有一天公司来了查一个星期数据的需求!我花了一个小时多才弄出来,所以记下来,下次遇到了,也记得...
当天
Select * From my_table a Where trunc(create_date)
= trunc(Sysdate);
当月的
Select * From my_table a Where to_char(create_date,'yyyymm')
= to_char(Sysdate,'yyyymm');
当星期的
trunc(Sysdate,'d')这是这个星期的第一天。
Next_day(trunc(sysdate,'d'),7) 这是这个星期的最后一天。
Select * From my_table a Where trunc(create_date)>=trunc(Sysdate,'d')
AND trunc(create_date)<= Next_day(trunc(sysdate,'d'),7)
当月的数据
select * from CDD_SITE t
where t.add_time >=TRUNC(SYSDATE, 'MM')
and t.add_time<=last_day(SYSDATE)
当年的数据
select * from CDD_SITE t
where t.ADD_TIME >=trunc(sysdate,'YYYY')
and t.ADD_TIME<=add_months(trunc(sysdate,'YYYY'),12)-1
查当年的数据,并且按月份分组
select count(*) from CDD_SITE site where 1 = 1 and site.ADD_TIME >=trunc(sysdate,'YYYY')
and site.ADD_TIME<=add_months(trunc(sysdate,'YYYY'),12)-1
group by TO_CHAR(site.add_time,'MM') ORDER BY TO_CHAR(site.add_time,'MM') asc