2017-05-26 53 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 
相關問題