Sql 共享一周中同一天的同一表格中的多个计数

Sql 共享一周中同一天的同一表格中的多个计数,sql,Sql,我有一张桌子,上面有: 一个ID-recid,例如123456 解析日期-解析日期时间,例如UTC 2014年2月28日凌晨12:00 创建日期-创建日期时间,例如UTC 2014年2月28日凌晨12:00 我试图在一周中的某一天确定,有多少是在这一天创建和解决的 例如,日期范围可能是2014年2月24日至2014年2月28日。我希望看到的数据类型是: =================================================== || DayOfWeek

我有一张桌子,上面有:

一个ID-recid,例如123456 解析日期-解析日期时间,例如UTC 2014年2月28日凌晨12:00 创建日期-创建日期时间,例如UTC 2014年2月28日凌晨12:00

我试图在一周中的某一天确定,有多少是在这一天创建和解决的

例如,日期范围可能是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)
关于如何让这个查询开始提取已解析计数的任何提示都将非常有用。提前非常感谢-乐意回答任何问题或在必要时提供更多信息

更新: 在这件事上还是没有运气。
有人吗?

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

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);

我在查询中遗漏了您的订单。你最好在明日前下订单。如果这不起作用,还有其他可能性。

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

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);

如果需要日期范围,请添加where子句。您可以将其添加到分组依据之前。不知何故,我在你的问题中的查询中遗漏了这一点。你是如何设法在子查询中有两个“否”,三个“有相同名称的拼写错误列”的?WHERE子句不也应该在子查询中吗?WHERE子句上的日期数学可以转换为使用索引。您正在SQL Server上的时间戳上使用包含上限…@sequelschmequel。我似乎只提出了我所想的问题。此版本应该是您正在寻找的。@GordonLinoff-两个错误-datename函数需要两个参数,并且“i”附近的语法不正确以UTC格式存储时间戳非常好。。。但是,本质上,这会根据浏览器所说的来源给出不同的结果?这似乎是混淆的时机成熟了:如果所有时间戳都被强制设置为最接近的UTC开始时间,那么您和我可能在不同的日子拥有相同的号码,如果他们不是您和我,则答案完全不同。这是有意的吗?在一个范围可能有多个偏移量的DST情况下,期望的行为是什么!?您的报告至少应该明确列出使用的偏移量,并且可能能够切换。我更喜欢UTC报告。嘿,伙计。应用程序将所有内容存储在UTC中,然后使用该报告按时区显示时间。时区被捕获为运行报告的用户的一个参数,因此,在周一,老板运行报告,但在他的查询中不包括周末,当他在另一个时区度假时,没有人工作/减少事故。然后,他给人们打电话,对他们大喊大叫,问他们为什么周一和周五的数字这么低/周末上班。时区转换管理层喜欢这种报告,它被称为记录那些没有完成配额的工人。人们应该使用同一组数字;要么报告UTC,要么报告目标的时区,不管是什么。很好。我可以考虑把时区硬编码。感谢您的反馈: