来自同一表的多个计数,它们共享一周中的同一天

时间:2014-02-28 01:58:08

标签: sql

我有一张表格,其中包含以下内容:

ID - 例如123456个
已解决的日期 - ResolvedDateTime,例如02/28/2014 12:00 AM(UTC)
创建日期 - CreatedDateTime例如02/28/2014 12:00 UTC(UTC)

我试图在一周中确定当天创建和解决的数量。

例如,日期范围可以是2014年2月24日到2014年2月28日。我想看的数据类型是:

  ===================================================
  ||    DayOfWeek || CreatedCount || ResolvedCount ||
  ||          Mon || 51           || 12            ||
  ||          Tue || 61           || 32            ||
  ||          Wed || 53           || 90            ||
  ||          Thu || 23           || 40            || 
  ===================================================

到目前为止,我有以下内容让我回到了本周的某一天和创建的计数。这是从之前存在的东西中反过来设计的:

/* Declare and set the browser timezone difference since we are manually setting a date */
declare @offset int; 
set @offset = (@BrowserTimezoneOffSet);

/* This will return by the day of the week */
SELECT left(DATENAME(dw,DATEADD(mi,@offset,CreatedDateTime)), 3) as createddatetime
       ,count(recid) as createdc
FROM Incident
WHERE DATEADD(mi,@offset,CreatedDateTime) >= @st_datein
AND DATEADD(mi,@offset,CreatedDateTime) <= @en_datein
GROUP BY 
  left(DATENAME(dw, DATEADD(mi,@offset,CreatedDateTime)), 3)
  ,left(DATEPART(dw, DATEADD(mi,@offset,CreatedDateTime)), 3)
ORDER BY
  left(DATEPART(dw, DATEADD(mi,@offset,CreatedDateTime)), 3)

有关如何让此查询开始撤回已解决的计数的任何提示都会很棒。非常感谢提前 - 很高兴回答任何问题或在必要时提供更多信息。

更新: 在这方面仍然没有运气。 那里有人吗?

2 个答案:

答案 0 :(得分:1)

这非常接近你想要的。它使用子查询为每个日期创建一行,然后总结:

SELECT left(DATENAME(dw, thedate), 3) as thedow,, sum(created) as created, sum(resolved) as resolved
FROM ((select DATEADD(mi, @offset, CreatedDateTime) as thedate, 1 as created, 0 as created
       from Incident i
       WHERE DATEADD(mi, @offset, CreatedDateTime) >= @st_datein AND DATEADD(mi, @offset, CreatedDateTime) <= @en_datein
      ) union all
      (select DATEADD(mi, @offset, ResolvedDateTime) as thedate, 1 as created, 0 as created
       from Incident i
       WHERE DATEADD(mi, @offset, CreatedDateTime) >= @st_datein AND DATEADD(mi, @offset, CreatedDateTime) <= @en_datein
      )
     ) i
GROUP BY left(DATENAME(dw, thedate), 3)
ORDER BY left(DATEPART(dw, DATEADD(mi, @offset, thedow)), 3);

我将order by留在了查询中。拥有order by min(thedate)可能会更好。如果这不起作用,还有其他可能性。

答案 1 :(得分:0)

以下是我解决这个问题的方法:

declare @st_date datetime;
declare @en_date datetime;
set @st_date = (@st_datein);
set @en_date = (@en_datein);

declare @offset int;
--set @offset = (@BrowserTimezoneOffset);
set @offset = 600;

with daterange(dt, dow, daynum) as
(select 
    @st_date AS [dt]
    ,LEFT(DATENAME(dw, @st_date),3) AS [dow]
    ,DATEPART(dw,@st_date) as [daynum]
 union all
 select 
    DATEADD(dd, 1, [dt]) as [dt] 
    ,LEFT(DATENAME(dw,DATEADD(dd,1,[dt])),3) as [dow]
    ,DATEPART(dw,DATEADD(dd,1,[dt])) as [daynum]
 from 
    daterange
 where 
    dt <= DATEADD(dd, -1, @en_date)
)
select
    dow
    ,daynum
    ,inc.createdc as createdcount
    ,inr.resolvedclosedc as resolvedclosedcount
from 
    daterange left outer join
(select
    left(DATENAME(dw,DATEADD(mi,@offset,CreatedDateTime)), 3) as createddatetime
    ,count(recid) as createdc
 from 
    Incident
 where 
    DATEADD(mi,@offset,CreatedDateTime) >= @st_date
    and DATEADD(mi,@offset,CreatedDateTime) <= @en_date
 group by  
    left(DATENAME(dw, DATEADD(mi,@offset,CreatedDateTime)), 3)
) as inc
on inc.CreatedDateTime = dow
left outer join
(select
    left(DATENAME(dw, DATEADD(mi,@offset,ResolvedDateTime)), 3) as ResolvedDateTime
    ,count(case when status in ('Resolved', 'Closed') then 1 end) as resolvedclosedc
 from 
    Incident
 where 
    DATEADD(mi,@offset,ResolvedDateTime) >= @st_date 
    and DATEADD(mi,@offset,ResolvedDateTime) <= @en_date 
 group by
    left(DATENAME(dw, DATEADD(mi,@offset,ResolvedDateTime)), 3)
) as inr
on inr.ResolvedDateTime = dow
group by
    dow
    ,daynum
    ,inr.resolvedclosedc
    ,inc.createdc
ORDER BY
    daynum
OPTION (MAXRECURSION 32767);