I have two tables, Link and Hit. The Hit table contains information about when links from Link were hit. The Link table contains a column HitsCount with the number of times each link has been hit. We regularly aggregate Hit's data into aggregations of data for each hour of hits, and remove the aggregated data to save space. When we aggregate, we want to also update Link.HitsCount with the COUNT() of links for the current hour we are aggregating over. How is this done?
Here is my current attempt, using T-SQL:
DECLARE cc CURSOR LOCAL FOR
SELECT COUNT(*) AS c, h.LinkId AS id
FROM Hit AS h
WHERE h.[Timestamp] >= @p_startTime AND h.[Timestamp] < @p_endHour
GROUP BY h.LinkId
OPEN cc
FETCH cc
UPDATELink
SET Link.HitsCount = HitsCount + c
FROM Link
WHERE CURRENT OF cc
CLOSE cc
DEALLOCATE cc
However, I get the error "Invalid column name 'c'." Also, this approach does not JOIN ON h.LinkId and Link.LinkId like it should. I've thought about dropping and populating a scratch table with the LinkId and COUNT(), but I'd rather avoid this. On the other hand, I'd also rather avoid a CURSOR.
Many thanks.
Nathan