I have a number of tables with a contributor and hits column, what I need to do is find the top 5 contributors that have had the most hits.
I am using the below code but it doesn't work, does nyone know what terrible mistake I am making?
--Create a temporary table
CREATE TABLE #TempItems
(
ID int IDENTITY,
Hits int,
Contributor int
)
-- Insert the rows from tblItems into the temp. table
INSERT INTO #TempItems (contributor,hits)
SELECT contributor,hits
FROM careers
WHERE active=1
UNION
SELECT contributor,hits
FROM going_out
WHERE active=1
UNION
SELECT contributor,hits
FROM mag
WHERE active=1
UNION
SELECT contributor,hits
FROM mind_body_soul
WHERE active=1
UNION
SELECT contributor,hits
FROM sound_vision
WHERE active=1
UNION
SELECT contributor,hits
FROM sport_rec
WHERE active=1
UNION
SELECT contributor,hits
FROM travel
WHERE active=1
-- Find out the first and last record we want
-- Now, return the set of paged records, plus, an indiciation of we
-- have more records or not!
SELECT contributor,sum(hits)
FROM #TempItems
WHERE ID < 6 group by contributor
cheers
I am using the below code but it doesn't work, does nyone know what terrible mistake I am making?
--Create a temporary table
CREATE TABLE #TempItems
(
ID int IDENTITY,
Hits int,
Contributor int
)
-- Insert the rows from tblItems into the temp. table
INSERT INTO #TempItems (contributor,hits)
SELECT contributor,hits
FROM careers
WHERE active=1
UNION
SELECT contributor,hits
FROM going_out
WHERE active=1
UNION
SELECT contributor,hits
FROM mag
WHERE active=1
UNION
SELECT contributor,hits
FROM mind_body_soul
WHERE active=1
UNION
SELECT contributor,hits
FROM sound_vision
WHERE active=1
UNION
SELECT contributor,hits
FROM sport_rec
WHERE active=1
UNION
SELECT contributor,hits
FROM travel
WHERE active=1
-- Find out the first and last record we want
-- Now, return the set of paged records, plus, an indiciation of we
-- have more records or not!
SELECT contributor,sum(hits)
FROM #TempItems
WHERE ID < 6 group by contributor
cheers