【发布时间】:2014-08-20 19:04:43
【问题描述】:
我是新来的,也是 sql 的菜鸟。我需要有关此逻辑的帮助。我相信问题出在“dateadd”功能中。我需要获取当前日期(2014 年 8 月)到 2 年前(2012 年 8 月)的日期范围。我的一个伙伴说我可以使用 max 函数,但他也不知道该怎么做。不知道该怎么做..请指导我。谢谢!
--count of EDI exceptions daily with current status
select count(InvoiceUniqueness) as [Nbr], CONVERT(varchar(12), ProcessDate, 101) as [Date],
case currentstatus when 1 then 'production' when 2 then 'exception' when 3 then 'archive' end as [CurrentStatus]
from PreProcessTransLog
where (CONVERT(DATETIME, ProcessDate, 102)) >= dateadd (yy,-2,getdate()) and InitialStatus =2
group by CONVERT(varchar(12), ProcessDate, 101), currentstatus
order by CAST (CONVERT(varchar(12), ProcessDate, 101) as smalldatetime) desc
查询结果:
6 10/11/2014 production
12 10/10/2014 production
3 09/30/2014 production
2 09/28/2014 production
34 09/27/2014 production
39 09/26/2014 production
150 08/02/2014 exception
40 08/01/2014 production
62 08/01/2014 archive
437 08/01/2014 exception
60 07/31/2014 production
54 07/31/2014 archive
46 07/31/2014 exception
61 07/30/2014 exception
113 07/30/2014 production
98 07/30/2014 archive
7 07/29/2014 exception
130 07/29/2014 archive
80 07/29/2014 production
84 07/28/2014 production
....
....
....
如您所见,我将在 8 月之后获得日期,例如 9 月和 10 月。我只想当前日期。
谢谢!!
【问题讨论】:
标签: sql sql-server date range max