这一直很令人费解,希望有人可以赐教。我有一个存储过程,当我从SSMS运行它时返回预期(至少对我来说)结果,并且当我从SQL作业运行它时返回意外结果。服务器正在运行SQL Server 2005,不确定sp版本。如果它有帮助我可以得到它。
当我从SSMS运行下面的代码时,只有一条记录,Mon,Mar 05:,包含InsufficientSampleSize的'True',正如我所料。
当我从SQL作业运行相同的代码时,所有记录都包含InsufficientSampleSize的“True”,这是意外的。
我是如何评估d.ScoreCount值的?也许这是一个像ANSI_NULLS这样的数据库设置,当我使用Job运行程序时会有所不同?我的数据目前不包含任何空值,所以我认为它不是ANSI_NULLS。
提前致谢。我尝试过多次重写这个逻辑,但我仍然遇到这个问题。
create table #tmpScoreData (EventDate datetime, Score int)
insert into #tmpScoreData values('20120302', 4)
insert into #tmpScoreData values('20120302', 7)
insert into #tmpScoreData values('20120302', 7)
insert into #tmpScoreData values('20120302', 2)
insert into #tmpScoreData values('20120302', 7)
insert into #tmpScoreData values('20120303', 9)
insert into #tmpScoreData values('20120303', 2)
insert into #tmpScoreData values('20120303', 4)
insert into #tmpScoreData values('20120303', 2)
insert into #tmpScoreData values('20120303', 5)
insert into #tmpScoreData values('20120304', 8)
insert into #tmpScoreData values('20120304', 8)
insert into #tmpScoreData values('20120304', 7)
insert into #tmpScoreData values('20120304', 2)
insert into #tmpScoreData values('20120304', 5)
insert into #tmpScoreData values('20120305', 7)
insert into #tmpScoreData values('20120305', 6)
insert into #tmpScoreData values('20120305', 2)
insert into #tmpScoreData values('20120306', 7)
insert into #tmpScoreData values('20120306', 2)
insert into #tmpScoreData values('20120306', 4)
insert into #tmpScoreData values('20120306', 2)
insert into #tmpScoreData values('20120306', 5)
insert into #tmpScoreData values('20120307', 5)
insert into #tmpScoreData values('20120307', 2)
insert into #tmpScoreData values('20120307', 8)
insert into #tmpScoreData values('20120307', 7)
insert into #tmpScoreData values('20120307', 5)
insert into #tmpScoreData values('20120308', 9)
insert into #tmpScoreData values('20120308', 4)
insert into #tmpScoreData values('20120308', 8)
insert into #tmpScoreData values('20120308', 3)
insert into #tmpScoreData values('20120308', 5)
declare @weekEnding datetime
set @weekEnding = '20120308'
select EventDate = cd.WeekDayAbbr + ', ' + substring(convert(varchar(10), cd.CalendarDate, 107), 1, 6) + ':',
AvgScore = d.Score,
InsufficientSampleSize = case when isnull(d.ScoreCount, 0) < 5 then 'True' end,
WeekEnding = @weekEnding
from (select 1 as ID, 'Fri' as WeekDayAbbr, @weekEnding - 6 as CalendarDate
union
select 2, 'Sat', @weekEnding - 5
union
select 3, 'Sun', @weekEnding - 4
union
select 4, 'Mon', @weekEnding - 3
union
select 5, 'Tue', @weekEnding - 2
union
select 6, 'Wed', @weekEnding - 1
union
select 7, 'Thu', @weekEnding) as cd
left join (select EventDate,
avg(convert(decimal(9,4), Score)) as Score,
count(Score) as ScoreCount
from #tmpScoreData
where EventDate <= @weekEnding
and EventDate > @weekEnding - 7
group by EventDate) as d on cd.CalendarDate = d.EventDate
order by cd.ID
drop table #tmpScoreData
我将日期更新为不太模糊的值。在我的实际存储过程中,日期在一个exising表中作为datetime值。所以,这不是我的问题的原因。感谢。
答案 0 :(得分:2)
您的日期可能会有不同的解释。 '3/8/2012'
是指8月3日还是3月8日?
为避免歧义,请不要只使用字符串和隐式转换。而是使用字符串和显式转换......
CONVERT(DATETIME, '3/8/2012', 103) -- Or whichever format is relevant
有关您需要的格式,请参阅CAST and CONVERT (Transact-SQL)。