如何从日期列中剥离时间并使每个日期的用户数唯一

时间:2018-10-12 15:03:02

标签: datetime sql-server-2014

我有一个事件表,其中包含太多列。我想查询3列,DateTimeResolved,团队和个人。首先,我想将DateTimeResolved列中的时间与日期分开。然后,我想知道自2017年4月以来,每个团队有多少个人(唯一计数)解决了事件。

可以做到吗?

我已经完成了以下查询,但是显然它们需要调整,因为它们没有给我我想要的最终结果。

select stat_datetimeresolved, resolvedbyteam, resolvedby 
from [dbo].[Incident]
where Stat_DateTimeResolved is not null
and Stat_DateTimeResolved >= '2017-04-01 00:00:00'
order by Stat_DateTimeResolved asc


select CAST(stat_datetimeresolved as date),
COUNT(resolvedby)
from [dbo].[Incident]
group by CAST(Stat_DateTimeResolved as DATE)
order by CAST(stat_datetimeresolved as DATE) asc

2 个答案:

答案 0 :(得分:1)

你很近;您可以将DISTINCT关键字添加到COUNT中,以按天获取唯一身份的团队成员数。

import cv2
import numpy as np

img = cv2.imread('croses.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
equ = cv2.equalizeHist(gray)
_, thresh = cv2.threshold(equ,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
kernel = np.ones((2,2),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
_, contours, hierarchy = cv2.findContours(opening,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

for cnt in contours:
    x,y,w,h = cv2.boundingRect(cnt)
    if w > 40 and h > 40:
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
        cv2.circle(img,(int(x+(w/2)), int(y+(h/2))),3,(0,0,255),-1)

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

答案 1 :(得分:0)

您的问题中有多个不兼容的请求。我认为您追求的实际上是:

自2017年4月以来,每个团队每天有多少人制定决议?

为此,您只需要返回一个唯一的列表,其中包含解决日期,个人所属团队以及实际做出解决的人,然后进行汇总:

with r as
(
    select distinct cast(stat_datetimeresolved as date) as DateResolved
                   ,ResolvedByTeam
                   ,ResolvedBy
    from [dbo].[Incident]
    where Stat_DateTimeResolved >= '20170401'  -- NULL check is redundant if you are filtering for a value
)
select DateResolved
      ,ResolvedByTeam
      ,count(ResolvedBy) as NumIndividualsMakingResolutions
from r
group by DateResolved
        ,ResolvedByTeam
order by DateResolved
        ,ResolvedByTeam;