【发布时间】:2020-10-22 23:41:08
【问题描述】:
目前我有一个查询可以获取上个月的数据。
我需要更改此设置,以便我可以选择一个选定的日期范围,以便我输入 2 个日期并拉回它们之间的所有结果。
在下面查看我的查询:
select * from Table1 I
inner join Service K on I.Service_Key = K.Service_Key
inner join Status S on I.Status_Key = S.Status_Key
where K.Service_Key = '1'
and S.Status_Name = 'Closed'
and month(I.Date_Key) = (select case when Month (GETDATE())-1 = 0 then 12 else Month (GETDATE())-1 end)
and year(I.Date_Key) = (select case when Month (GETDATE()) -1 = 0 then year (GETDATE()) -1 ELSE YEAR (GETDATE()) end)
我需要能够说出 dd/mm/yy 和 dd/mm/yy 之间的日期
【问题讨论】:
-
旁白:您的日期处理不是sargable,即它不能从
Date_Key上的索引搜索中受益。计算开始和结束日期并使用它们,如 Foster90 和 Linoff 博士的回答,允许索引搜索。