通过将下面的TeamRecords = 1更改为=另一个数字来查找具有我更改的数量的行,它有时仅计数太多,这是奇数。创建新事件时,它具有唯一编号,每次从任务表添加新分配时,它会添加IncidentNumber的另一行,因此您可以使用下面的seq = 1删除重复的事件编号行。当创建新的赋值时,它会在Task表中创建一个新的CreateddateTime,例如你可以执行Max(t。[CreatedDateTime]来查找任何IncidentNumber的最后一个赋值。所以,TeamRecords = 1是我需要找到的该特定团队的所有记录,其中该团队只有1个任务。
这对你有帮助吗? 这是我到目前为止所拥有的......
Use TEST
Go
WITH RankResult AS
(
SELECT i.[IncidentNumber],
i.[CreatedDateTime],
i.[ResolutionDateAndTime],
i.[Priority],
i.[Status],
i.[ClientName],
i.[ClientSite],
t.[OwnerTeam],
t.[Owner],
row_number() over( partition by i.RecID
order by t.CreatedDateTime desc, t.OwnerTeam ) seq,
TeamRecords = COUNT(*) OVER(PARTITION BY t.ParentLink_RecID)
FROM Incident as i
Inner JOIN Task as t
ON i.RecID = t.ParentLink_RecID
WHERE t.OwnerTeam = 'Infrastructure Services'
AND i.CreatedDateTime >= '20121001'
AND i.CreatedDateTime <= '20131001'
)
SELECT DISTINCT
[IncidentNumber],
[CreatedDateTime],
[ResolutionDateAndTime],
[Priority],
[Status],
[ClientName],
[ClientSite],
[OwnerTeam],
[Owner]
FROM RankResult
Where TeamRecords = 1
And Seq = 1
Order By IncidentNumber Asc
GO
答案 0 :(得分:0)
您始终可以使用一个简单的子查询,该子查询在相关列中进行分组,计算它们,计数为1的过滤器,然后将其连接回主表以选择适当的行:
SELECT Incident.*
FROM (
SELECT OwnerTeam
FROM Incident AS Inc1
GROUP BY OwnerTeam
HAVING COUNT( * ) = 1
) AS Team
, Incident
WHERE Incident.OwnerTeam = Team.OwnerTeam
(但是,如果没有更多信息,很难说这是否适合您。)
答案 1 :(得分:0)
使用ROW_NUMBER意味着您将返回每个团队的第一个作业,而不一定只有一个作业的团队。为此,您可以使用COUNT(*) OVER()
:
WITH RankResult AS
(
SELECT i.[IncidentNumber],
i.[CreatedDateTime],
i.[ResolutionDateAndTime],
i.[Priority],
i.[Status],
i.[ClientName],
i.[ClientSite],
t.[OwnerTeam],
t.[Owner],
TeamRecords = COUNT(*) OVER(PARTITION BY i.RecID)
FROM Incident as i
INNER JOIN Task as t
ON i.RecID = t.ParentLink_RecID
WHERE t.OwnerTeam = 'Info Services'
AND i.CreatedDateTime >= '20121001'
AND i.CreatedDateTime <= '20131001'
)
SELECT DISTINCT
[IncidentNumber],
[CreatedDateTime],
[ResolutionDateAndTime],
[Priority],
[Status],
[ClientName],
[ClientSite],
[OwnerTeam],
[Owner]
FROM RankResult
WHERE TeamRecords = 1;
要注意的是,除了分析功能之外,我已经改变了两件事。首先,我已将您的日期更改为与文化无关的格式yyyyMMdd
,yyyy-MM-dd
仍然可能不明确,因此2013-01-02
可能是2月1日或1月1日,具体取决于您的服务器/会话设置。其次,无论如何,你的cluase将你的联接转变为INNER JOIN
,所以我只是把它变成INNER JOIN
:
FROM Incident as i
LEFT JOIN Task as t
ON i.RecID = t.ParentLink_RecID
WHERE t.OwnerTeam = 'Info Services'
AND i.CreatedDateTime >= '20121001'
此处,如果Task
中没有匹配,则OwnerTeam
将NULL
且NULL = 'Info Services'
的评估结果为false,因此您永远不会返回任何匹配的行Task
因此使其成为内部联接。如果你确实想要LEFT JOIN,那么你需要将这个子句移动到JOIN:
FROM Incident as i
LEFT JOIN Task as t
ON i.RecID = t.ParentLink_RecID
AND t.OwnerTeam = 'Info Services'
WHERE i.CreatedDateTime >= '20121001'