我正在创建一个存储过程,并且在编写逻辑时遇到一些麻烦。
我有一个包含队列名称列表的表,例如(错误,支持等)。
我正在制作一个页面,显示所有队列以及每个队列中的门票总数。
我首先创建了一个临时表,并使用队列名列表填充它。我现在试图弄清楚如何使用每个队列名称的所有计数列表来更新临时表。
在下面的示例中,有4个队列和1个票证。但是它说每个队列有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;
答案 0 :(得分: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