2017-05-26 39 views
1

WorkQueItem包含唯一请求,其中一些失败且发生异常,其他失败。如何排除与特定值无关的结果

标记表返回与请求相关的所有标记。一个请求可以有多个标签或没有。

我试图得到所有的失败请求的数量,按照没有“Forwarded”标签的异常类型分组。 我确实得到了'重复请求'和'虚拟请求',它们没有“转发”标签,但'无法完成'确实有'转发'标签,但它也有'延迟'标签, 。

因为它与某处的'Forwarded'相关联,所以我不想得到'Could not Complete'。

我怎么能得到没有与他们关联的转发标签的请求?

通缉的结果:

WorkQueueItem含有独特的要求,有的失败,异常和其他人并没有失败。

标记表返回与请求相关的所有标记。一个请求可以有多个标签或没有。

我试图得到所有的失败请求的数量,按照没有“Forwarded”标签的异常类型分组。我确实得到了'重复请求'和'虚拟请求',它们没有“转发”标签,但'无法完成'确实有'转发'标签,但它也有'延迟'标签以及返回的标签。

因为它与某处的'Forwarded'相关联,所以我不想得到'Could not Complete'。

我怎么能得到没有与他们关联的转发标签的请求?

通缉的结果:(我之前添加的标签栏只是为了帮助理解是怎么回事,我不希望它)

+---+--------------------+-------+ 
| | 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 | 
+---+----------------+-------+ 

TAG

+---+-------+-----------+ 
| | id | tag  | 
+---+-------+-----------+ 
| 1 | 14904 | Deferred | 
+---+-------+-----------+ 
| 2 | 14905 | Forwarded | 
+---+-------+-----------+ 
| 3 | 14906 | Deferred | 
+---+-------+-----------+ 
+1

可以添加一个样本数据集 – scsimon

+1

我加了3个样品,这是否帮助? – Carol

+0

是的,绝对!最后一件事,你展示了你目前正在获得的东西,但更有利的是你想要的结果。 – scsimon

回答

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

看起来像你找谁g的条件汇总如下:

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 
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 
相关问题