大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1、最近一月比较简单,取当前时间的年份和月份,拼接成当前月份1号。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:申请域名、网站空间、营销软件、网站建设、雷州网站维护、网站推广。
2、最后一周:取当前的时间为参考,用datepart取出时间的周编号,进行筛选,获取最近周1的日期,比如,表格a,日期字段rq
select max(rq) from 表格a
where datepart(weekday,rq) = 2 and rq 当前时间 按到 rq 本月1号;
指定一个起始日,计算到该日的天数,/7 得到经过的周数。然後按这个周数分组。
select Inspect_name,
A=sum(A), B=sum(B),双休日=sum(case when 双休日 = 2 then 1 else 0 end)
from
(
select weekly,Inspect_name,
A=sum(A), B=sum(B),双休日=sum(case when 双休日119 then 1 else 0 end)
from
(
select weekly, Inspect_name, vdatetime2,
sum(case when rank= 'A' then 1 else 0 end) A,
sum(case when rank= 'B' then 1 else 0 end) B,
SUM(CASE WHEN xxx = '休' THEN vtime ELSE 0 END) AS 双休日
from
(
select datepart(wk,(case when convert(varchar(16),vdatetime2,120) between convert(varchar(10),vdatetime2,120)+' 00:00' and convert(varchar(10),vdatetime2,120)+' 05:01' then vdatetime2 -1
else vdatetime2 end)) as weekly,Inspect_name, rank,
(case when convert(varchar(16),vdatetime2,120) between convert(varchar(10),vdatetime2,120)+' 00:00' and convert(varchar(10),vdatetime2,120)+' 05:01' then convert(varchar(10),vdatetime2 -1,120)
else convert(varchar(10),vdatetime2,120) end) as vdatetime2,
( CASE WHEN(datepart(dw, vdatetime2)=6 AND CONVERT(VARCHAR(8),vdatetime2,108)'23:00') OR (datepart(dw, vdatetime2)=7 )or (datepart(dw, vdatetime2)=1 ) OR(datepart(dw, vdatetime2)=2 AND CONVERT(VARCHAR(8),vdatetime2,108) '04:59')THEN '休' ELSE '不休' END) as 'xxx',
vtime
from tb
) t
group by weekly,Inspect_name, vdatetime2
) Tbl
group by weekly,Inspect_name
) tbl1
group by Inspect_name
DATEPART(ww,DATE) 函数计算的周次讲解:
这个函数是以周日为每个周次的第一天,所以你要实现周一为每周的开始必须这样书写才能实现
DATEPART(ww,DATE-1)
---把getdate()替换成你的日期字段就可以了, between and的2个日期分别是你说的2个年头年尾的情况
select case when getdate()=cast('2014/1/1' as datetime) then datepart(week,getdate()) else 1 end as 第几周
--from table_name
where getdate() between (case when datepart(weekday,cast('2014/1/1' as datetime))1 then
cast('2014/1/1' as datetime)-datepart(weekday,cast('2014/1/1' as datetime))+2
end )
and
(case when datepart(weekday,cast('2014/12/31' as datetime))6 then
cast('2014/12/31' as datetime)-datepart(weekday,cast('2014/12/31' as datetime))+1
end )
你这个涉及"mssql sqlserver set datefirst关键字的用法简介"
请仔细了解此函数的用法就可以了,如有疑问,及时沟通!