我是新来的,也是sob的一个菜鸟。我需要有关这个逻辑的帮助。问题出在" dateadd"功能我相信。我需要获取当前日期(2014年8月)到2年前(2012年8月)的日期范围。我的一位同事说我可以使用最大功能,但他不知道如何。不知道该怎么做..请指导我。谢谢!
--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月之后得到的日期就像九月和十月一样。我只想要约会。
谢谢!
答案 0 :(得分:2)
你能加入ProcessDate< =今天的日期吗?
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 ProcessDate <= GETDATE()
AND InitialStatus = 2
GROUP BY CONVERT(VARCHAR(12), ProcessDate, 101) ,
currentstatus
ORDER BY CAST (CONVERT(VARCHAR(12), ProcessDate, 101) AS SMALLDATETIME) DESC