2015-06-03 212 views
1

我有一个存储过程,我创建并具有想出逻辑有点麻烦计数。TSQL更新表与其他

我有一个包含队列名称,如(错误,支持,等等)的列表的表。

我提出是要告诉所有队列,在每一个票总数沿着页面。

我首先创建一个临时表,并与队列名称的列表填充它。我现在试图找出如何使用每个队列名称的所有计数列表来更新该临时表。

在下面的例子中,有4个队列和1票。然而它的说法是每个队列有一张票是不正确的。

这个没有更好的办法?

-- Create a temp table to hold all of the queues and counts 
DECLARE @table AS TABLE (
    reqQueue  VARCHAR (100), 
    totalRecords INT   NULL); 

-- Load our temp table with the data 
INSERT INTO @table (reqQueue) 
SELECT reqQueue 
FROM apsSupport_queues; 

-- Update the counts for each of the queues 
UPDATE @table 
    SET totalRecords = (SELECT COUNT(reqID) 
         FROM apsSupport_tickets AS t 
         WHERE t.reqQueue = reqQueue) 
WHERE reqQueue = reqQueue; 

回答

1

无需临时表:

select reqQueue, count(t.reqId) as TotalRecords 
from 
apsSupport_queues q 
left join 
apsSupport_tickets t 
on q.reqQueue = t.ReqQueue 
group by q.reqQueue 
+0

嗯,我想,和我所有的队列中有1总记录,但只有1中apsSupport_tickets表 – SBB

+0

票改计行到'计数(t.reqID)AS totalRecords',它工作正常 – SBB

+0

是的 - 错过了,更新的答案。 – dugas