Sql server Microsoft SQL计算待办事项

Sql server Microsoft SQL计算待办事项,sql-server,tsql,Sql Server,Tsql,我想计算一下上个月每周的积压工作。日期格式为MM/DD/YY | mutation | issued_date | queryno_i | status | ----------------------------------------------- 01/05/14 12/31/13 321 OPEN 01/02/14 08/01/13 323 CLOSED 01/01/14 06/06/13 123

我想计算一下上个月每周的积压工作。日期格式为MM/DD/YY

| mutation | issued_date | queryno_i | status |
-----------------------------------------------
  01/05/14   12/31/13       321         OPEN
  01/02/14   08/01/13       323         CLOSED
  01/01/14   06/06/13       123         OPEN
  01/01/14   01/01/14       1240        CLOSED
  01/02/14   01/01/14       1233        OPEN
  01/03/14   01/03/14       200         CLOSED
  01/05/14   01/04/14       300         OPEN
  01/06/14   01/05/14       231         OPEN
  01/07/14   01/06/14       232         CLOSED
  01/09/14   01/10/14       332         OPEN
  01/11/14   01/11/14       224         CLOSED
  01/15/14   01/14/14       225         CLOSED
  01/16/14   01/15/14       223         OPEN
我希望我的结果集如下所示:

WeekNum | Opened | Closed | Total Open
--------------------------------------
   1        4        3         4    <= (2-4)+ data in week 2 so (2-4)+(1-2)+7
   2        4        2         6    <= (1-2)+7           
   3        2        1         7    <= total count                   

上个月每周的积压工作。 我认为这是指过去4周,因为这似乎是你正在做的事情。 假设突变代表记录更新的日期,则可能会设置为关闭

因此,首先,我生成一个日期列表,这样即使没有新的/已关闭的记录,第X周也会有一个答案

declare @SundayJustGone datetime

-- We need to get rid of the time component, done through convert.
set @SundayJustGone = convert(date, dateadd(d, 1-DATEPART(dw, getdate()), getdate()))
-- If earlier than sql 2008, can get rid of time component through: set @SundayJustGone = SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, @SundayJustGone))

;with 
Last4Weeks as
(
-- Get the sunday of the week just gone.
select @SundayJustGone as SundayDate -- Sunday just gone

union all

select dateadd(d, -7, SundayDate) -- Get the previous Sunday
from Last4Weeks
where dateadd(d, -7, SundayDate) > dateadd(Wk, -4, @SundayJustGone) -- where the new date is not more than 4 weeks old
)
select A.SundayDate, 
    DATEPART(wk, DateAdd(d, -1, A.SundayDate)) as Week_Number, -- SQL considers Sunday the first day of the week, so we need to move it back 1 day to get the right week
    (select count(*) 
        from t.tech_query 
        where issued_date between DateAdd(d, -6, A.SundayDate) and A.SundayDate -- Was issued this week. (between monday - sunday)
        ) as Opened,
    (select count(*) 
        from t.tech_query 
        where status = 3 -- where it is closed
        and mutation between DateAdd(d, -6, A.SundayDate) and A.SundayDate -- and the mutation was this week. (between monday - sunday)
        ) as Closed,
    (select count(*) 
        from t.tech_query 
        where (status != 3 or datediff(d, mutation, A.SundayDate) < 0 ) -- Is still open, or was closed after this week.
        and datediff(d, issued_date, A.SundayDate) >= 0 -- and it was issued on or before the sunday.
    ) as TotalOpen
from Last4Weeks as A
希望这能有所帮助


结果与你的不同,因为我假设周一是一周的第一天。要将一周的开始改回星期日,星期六需要视为一周的结束,因此,将集合@SundayJustGone=convertdate,dateaddd,1-DATEPARTdw,getdate,getdate改为集合@SundayJustGone=convertdate,dateaddd,-DATEPARTdw,getdate,getdate 1-removed

最后一列的计算方式不清楚。你能澄清一下吗?我想计算最后一列的方法是计算所有打开的记录,然后从那里向后计算,计算一周的打开-关闭,并用这个数字从打开的总记录中减去。因此,对于第2周,总共有100个开放,第1周有20个关闭,10个开放,我将有第2周,总开放=90。您认为这可以在SQL中实现吗?我希望这是有意义的…谢谢你的帮助!!我花了一些时间去理解一切,但一切对我来说都是有意义的!!
declare @SundayJustGone datetime

-- We need to get rid of the time component, done through convert.
set @SundayJustGone = convert(date, dateadd(d, 1-DATEPART(dw, getdate()), getdate()))
-- If earlier than sql 2008, can get rid of time component through: set @SundayJustGone = SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, @SundayJustGone))

;with 
Last4Weeks as
(
-- Get the sunday of the week just gone.
select @SundayJustGone as SundayDate -- Sunday just gone

union all

select dateadd(d, -7, SundayDate) -- Get the previous Sunday
from Last4Weeks
where dateadd(d, -7, SundayDate) > dateadd(Wk, -4, @SundayJustGone) -- where the new date is not more than 4 weeks old
)
select A.SundayDate, 
    DATEPART(wk, DateAdd(d, -1, A.SundayDate)) as Week_Number, -- SQL considers Sunday the first day of the week, so we need to move it back 1 day to get the right week
    (select count(*) 
        from t.tech_query 
        where issued_date between DateAdd(d, -6, A.SundayDate) and A.SundayDate -- Was issued this week. (between monday - sunday)
        ) as Opened,
    (select count(*) 
        from t.tech_query 
        where status = 3 -- where it is closed
        and mutation between DateAdd(d, -6, A.SundayDate) and A.SundayDate -- and the mutation was this week. (between monday - sunday)
        ) as Closed,
    (select count(*) 
        from t.tech_query 
        where (status != 3 or datediff(d, mutation, A.SundayDate) < 0 ) -- Is still open, or was closed after this week.
        and datediff(d, issued_date, A.SundayDate) >= 0 -- and it was issued on or before the sunday.
    ) as TotalOpen
from Last4Weeks as A