TSQL 正在使用其他计数更新 Table

TSQL Updating Table with Counts from Other

我正在创建一个存储过程,但在想出逻辑时遇到了一些困难。

我有一个 table,其中包含一个队列名称列表,例如(错误、支持等)。

我正在制作一个页面,该页面将显示所有队列以及每个队列中的工单总数。

我首先创建了一个临时文件 table 并用队列名称列表填充它。我现在正试图弄清楚如何使用每个队列名称的所有计数列表来更新该临时 table。

在下面的示例中,有 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;

不需要温度 table:

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