WorkQueueItem包含唯一请求,有些请求失败并且其他请求失败。
Tag表返回与请求相关的所有标记。请求可以包含多个标记,也可以不包含任何标记。
我正在尝试按照没有“转发”标记的异常类型对所有失败请求进行分组。 我确实得到了“重复请求”和“虚拟请求”,它们没有“转发”标签,但“无法完成”确实有“转发”标签,但它也有这个“延期”标签,并且还会返回。
我根本不想得到'无法完成',因为它与某个地方的'转发'相关联。
如何获取没有与其关联的转发标记的请求?
通缉结果:
WorkQueueItem包含唯一请求,有些请求失败并且其他请求失败。
Tag表返回与请求相关的所有标记。请求可以包含多个标记,也可以不包含任何标记。
我正在尝试按照没有“转发”标记的异常类型对所有失败请求进行分组。我确实得到了“重复请求”和“虚拟请求”,它们没有“转发”标签,但“无法完成”确实有“转发”标签,但它也有这个“延期”标签,并且还会返回。
我根本不想得到'无法完成',因为它与某个地方的'转发'相关联。
如何获取没有与其关联的转发标记的请求?
通缉结果:(我之前添加了标签栏只是为了帮助理解发生了什么,我不想要它)
+---+--------------------+-------+
| | Exception | Count |
+---+--------------------+-------+
| 1 | Duplicate Request | 1 |
+---+--------------------+-------+
| 2 | Dummy Request | 1 |
+---+--------------------+-------+
目前的结果:
+---+--------------------+-------+----------+
| | Exception | Count | tag |
+---+--------------------+-------+----------+
| 1 | Duplicate Request | 1 | NULL |
+---+--------------------+-------+----------+
| 2 | Dummy Request | 1 | Deferred |
+---+--------------------+-------+----------+
| 3 | Could not Complete | 1 | Deferred |
+---+--------------------+-------+----------+
当前查询:
SELECT exceptionreason as Exception,
COUNT(exceptionreason) as Count,
tag
FROM [myDB].[dbo].[WorkQueueItem]
JOIN myDB.dbo.WorkQueue
ON WorkQueue.ident = WorkQueueItem.queueident
LEFT JOIN myDB.dbo.WorkQueueItemTag
ON WorkQueueItem.ident = WorkQueueItemTag.queueitemident
LEFT JOIN myDB.dbo.Tag
ON WorkQueueItemTag.tagid = Tag.id
WHERE name='TestQueue'
AND exception is not null
AND (NOT Tag.tag LIKE '%Forwarded%' OR Tag.tag is null)
Group by exceptionreason, tag
我希望这里有足够的信息。 感谢任何帮助。谢谢。
编辑: 添加数据集
WorkQueueItem
+---+--------+------------+--------------------+------------+
| | ident | exception | exceptionreason | completed |
+---+--------+------------+--------------------+------------+
| 1 | 192947 | 2017-05-25 | Dummy Request | NULL |
+---+--------+------------+--------------------+------------+
| 2 | 194990 | NULL | NULL | 2017-05-25 |
+---+--------+------------+--------------------+------------+
| 3 | 194994 | 2017-05-25 | Duplicate Request | NULL |
+---+--------+------------+--------------------+------------+
| 4 | 194995 | 2017-05-25 | Could not Complete | NULL |
+---+--------+------------+--------------------+------------+
WorkQueueItemTag
+---+----------------+-------+
| | queueitemident | tagid |
+---+----------------+-------+
| 1 | 192947 | 14904 |
+---+----------------+-------+
| 2 | 194995 | 14905 |
+---+----------------+-------+
| 3 | 194995 | 14906 |
+---+----------------+-------+
标签
+---+-------+-----------+
| | id | tag |
+---+-------+-----------+
| 1 | 14904 | Deferred |
+---+-------+-----------+
| 2 | 14905 | Forwarded |
+---+-------+-----------+
| 3 | 14906 | Deferred |
+---+-------+-----------+
答案 0 :(得分:1)
这可能接近你想要的。
SELECT
WQI.exceptionreason,
COUNT(*) [Total]
FROM
myDB.dbo.WorkQueueItem WQI
WHERE
WQI.exception IS NOT NULL
AND EXISTS(SELECT 1 FROM myDB.dbo.WorkQueue WHERE ident = WQI.queueident AND name = 'TestQueue')
AND NOT EXISTS(
SELECT
1
FROM
myDB.dbo.WorkQueueItemTag WQIT
WHERE
WQIT.queueitemident = WQI.ident
AND EXISTS(SELECT 1 FROM myDB.dbo.Tag WHERE id = WQIT.tagid AND tag LIKE '%Forwarded%')
)
GROUP BY
WQI.exceptionreason;
答案 1 :(得分:1)
一种简单的方法是使用相关的子查询和NOT IN
declare @WorkQueueItem table (ident int, exception datetime, exceptionreasion varchar(128), completed datetime)
insert into @WorkQueueItem
values
(192947,'2017-05-25','Dummy Request',NULL),
(194990,NULL,NULL,'2017-05-25'),
(194994,'2017-05-25','Duplicate Request',NULL),
(194995,'2017-05-25','Could not Complete',NULL)
declare @WorkQueueItemTag table (queueitemindent int, tagid int)
insert into @WorkQueueItemTag
values
(192947,14904),
(194995,14905),
(194995,14906)
declare @TAG table (id int, tag varchar(64))
insert into @TAG
values
(14904,'Deferred'),
(14905,'Forwarded'),
(14906,'Deferred')
select
i.exceptionreasion
,count(*)
from @WorkQueueItem i
where i.ident not in (select i.ident
from @WorkQueueItem i
left join @WorkQueueItemTag it on it.queueitemindent = i.ident
left join @TAG t on t.id = it.tagid
where t.tag = 'Forwarded' )
and i.exceptionreasion is not null
group by
i.exceptionreasion
答案 2 :(得分:0)
看起来您正在寻找条件聚合,如下所示:
SELECT exceptionreason as Exception,
SUM(case when Tag.tag LIKE '%Forwarded%' or Tag.tag is null then 0 else 1 end) as Count,
tag
FROM [myDB].[dbo].[WorkQueueItem]
JOIN myDB.dbo.WorkQueue
ON WorkQueue.ident = WorkQueueItem.queueident
LEFT JOIN myDB.dbo.WorkQueueItemTag
ON WorkQueueItem.ident = WorkQueueItemTag.queueitemident
LEFT JOIN myDB.dbo.Tag
ON WorkQueueItemTag.tagid = Tag.id
WHERE name='TestQueue'
AND exception is not null
----AND (NOT Tag.tag LIKE '%Forwarded%' OR Tag.tag is null)
Group by exceptionreason, tag