代码如下
select group_date groupDate from (SELECT
@cdate input_date,
@last_sat := date_add(date_sub(date_sub(@cdate,interval day(@cdate)-1 day),interval 1 month), interval 6-date_format(date_sub(date_sub(@cdate,interval day(@cdate)-1 day),interval 1 month),'%w') day) last_sat,
@this_sat := date_add(date_sub(@cdate,interval day(@cdate)-1 day), interval 6-date_format(date_sub(@cdate,interval day(@cdate)-1 day),'%w') day) this_sat,
@next_sat := date_add(date_add(date_sub(@cdate,interval day(@cdate)-1 day),interval 1 month), interval 6-date_format(date_add(date_sub(@cdate,interval day(@cdate)-1 day),interval 1 month),'%w') day) next_sat,
@last_point := if(day(@last_sat)-3>0,date_sub(@last_sat,interval 7 day),@last_sat) last_point,
@this_point := if(day(@this_sat)-3>0,date_sub(@this_sat,interval 7 day),@this_sat) this_point,
@next_point := if(day(@next_sat)-3>0,date_sub(@next_sat,interval 7 day),@next_sat) next_point ,
case
when @cdate >= @last_point and @cdate < @this_point then
concat(
'W',
datediff(@cdate,@last_point) div 7 + 1, '/',
year(@cdate),'-',
if(month(@last_sat)>=10,month(@last_sat),concat('0',month(@last_sat)))
)
when @cdate >=@this_point and @cdate < @next_point then
concat(
'W',
datediff(@cdate,@this_point) div 7 + 1,'/',
year(@cdate),'-',
if(month(@this_sat)>=10,month(@this_sat),concat('0',month(@this_sat))))
else
concat(
'W',
datediff(@cdate,@next_point) div 7 + 1,'/',
year(@cdate),'-',if(month(@next_sat)>=10,month(@next_sat),concat('0',month(@next_sat))))
end
group_date
from (SELECT @cdate := #{inputDate}) as gd
);
本季度的第一天,然后你可以把字符串再转换为日期。
方法很多,包括同一个函数的别名(同义词)也很多。比如CURDATE(),CURRENT_DATE(),
CURRENT_DATE,
NOW
等都可以返回当天
mysql教程>
select
CURDATE(),ELT(QUARTER(CURDATE()),
->
year(CURDATE())*1000+0101,
->
year(CURDATE())*1000+0401,
->
year(CURDATE())*1000+0701,
->
year(CURDATE())*1000+1001)
as
firstDayofQ
+------------+-------------+
|
CURDATE()
|
firstDayofQ
|
+------------+-------------+
|
2009-05-19
|
2009401
|
+------------+-------------+
1
row
in
set
(0.00
sec)
mysql>
用mysql语句获取本季度的第一天
本月的第一天,
date(
concat(year(curdate()),'-',month(curdate()),'-','1'))
本周的第一天
curdate()-
WEEKDAY(curdate())
用mysql语句获取本季度的第一天
易客CRM之前的版本中有一个报表是按月统计销售情况,最近有个客户想按周统计销售情况。按月统计的Sql语句比较好写,sql语句如下:SELECT DATE_FORMAT(ec_salesorder.duedate,’%Y-%m’) as m, sum(ec_salesorder.total) as total, count(*) as so_count FROM ec_salesorder GROUP BY m ORDER BY m,也就是把duedate日期以月的形式显示,然后groupby,那么按周如何统计呢?
搜了一下mysql的manual,在这里找到一个解决方法,通过mysql的week函数来做,sql语句如下:SELECT WEEK(ec_salesorder.duedate) as m, sum(ec_salesorder.total) as total, count(*) as so_count FROM ec_salesorder GROUP BY m ORDER BY m,这个方法有个缺陷,不能显示年份,仅仅靠一个周数不方便查看统计信息。
继续研究mysql manual,在DATE_FORMAT函数介绍发现2个格式符和周有点关系:
%X Year for the week where Sunday is the first day of the week, numeric, four digitsused with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digitsused with %v
把上面的Sql语句中改成:
SELECT DATE_FORMAT(ec_salesorder.duedate,’%x %v’) as m, sum(ec_salesorder.total) as total, count(*) as so_count FROM ec_salesorder GROUP BY m ORDER BY m
显示的结果如下:
m total so_count
2009 11 10000.00 3
2009 12 44000.00 5
如果周日为一周的第一天,那么sql语句应该为:
SELECT DATE_FORMAT(ec_salesorder.duedate,’%X %V’) as m, sum(ec_salesorder.total) as total, count(*) as so_count FROM ec_salesorder GROUP BY m ORDER BY m
结果应该没错,不出意外,易客CRM下个版本将增加按周统计销售情况的报表。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)