我在使用datepart的查询中遇到了一个问题。我们一直使用以下查询来返回最近三周的数据,但我们最近发现公司正在使用周一至周日的报告周,而以下查询默认为周日至周六。我尝试了"SET LANGUAGE BRITISH"
和"SET DATEFIRST 1"
,但我不能很好地掌握这些功能,因为它们不会更改我的查询结果。我应该提一下,我们正在运行sql server 2000.如果你知道一个解决方案,你的帮助将不胜感激:
declare @name varchar(50);
set @name = 'A name here';
SELECT week, year, CallCount, GoodCalls, CAST(CAST(GoodCalls as float)/CAST(CallCount as float)as decimal (18,4)) as NCP_perc
FROM
(SELECT TOP 3 datepart(ww, a.date_c) as week
,datepart(year, a.date_c) as year
,SUM(CallCount_wo_Xfer) as CallCount
,ROUND(SUM(CAST((CallCount_wo_Xfer*NCP_wo_Xfer)as float)),0) as GoodCalls
FROM db1 A
inner join db2 B
on a.Agent = b.Name collate database_default
inner join db3 C
on b.id = c.id collate database_default
where c.manager = @name
group by datepart(year, a.date_c), datepart(ww, a.date_c)) AS T
order by year desc, week desc
答案 0 :(得分:3)
星期一是第2天,请尝试
SET DATEFIRST 2
答案 1 :(得分:1)
如果您想从星期一开始,设置DateFirst 2将跳过前两天 使用 设置DateFirst 1