I want to show a third-level overview of how many to-do items are in various statuses (pending declined and completed) for the third level individuals.In this example there's 2 regional leaders (level3), each with local leaders (level2) and individual team members (level1)Status "1" is a new or pending to-do itemStatus "2" is a declined itemStatus "3" is a completed itemHere's what I want the output to look like (I may need to add columns for the final report but it should look as follows):LEVEL3 | Status 1 | Status 1 | Status 2 | Status 3 |LEADER | TOTAL | 2012 | TOTAL | TOTAL |-------|----------|----------|----------|----------|paul | 3 | 1 | 1 | 0 |roger | 1 | 1 | 1 | 0 |
Here's some sample data similar to my live DB:CREATE TABLE #items (id int, datestamp datetime, userid int, status int)INSERT INTO #items (id,datestamp,userid,status) VALUES (1,'12/26/2011',1,1)INSERT INTO #items (id,datestamp,userid,status) VALUES (2,'12/26/2011',2,1)INSERT INTO #items (id,datestamp,userid,status) VALUES (3,'1/2/2012',4,1)INSERT INTO #items (id,datestamp,userid,status) VALUES (4,'1/3/2012',1,2)INSERT INTO #items (id,datestamp,userid,status) VALUES (5,'1/4/2012',2,1)INSERT INTO #items (id,datestamp,userid,status) VALUES (6,'1/6/2012',4,2)CREATE TABLE #users ([id] int, [reportsto] int, [name] varchar(15) )INSERT INTO #users (id, reportsto, name) VALUES (1, 3, 'jason')INSERT INTO #users (id, reportsto, name) VALUES (2, 3, 'richard')INSERT INTO #users (id, reportsto, name) VALUES (3, 6, 'tammy')INSERT INTO #users (id, reportsto, name) VALUES (4, 5, 'steven')INSERT INTO #users (id, reportsto, name) VALUES (5, 7, 'gina')INSERT INTO #users (id, reportsto, name) VALUES (6, 0, 'paul')INSERT INTO #users (id, reportsto, name) VALUES (7, 0, 'roger')
I have a view called hierarchy that shows me the hierarchy for each level 1 user. Here's the source for the view:SELECT a.id 'level1',b.id 'level2',c.id 'level3'FROM #users aINNER JOIN #users b ON a.reportsto = b.idINNER JOIN #users c ON b.reportsto = c.id
What's the best way to accomplish this? I've run into table/view limits when using a straight query and timeouts when trying to create a cursor that looks through all level3 users and populates a temporary table with an INSERT and multiple UPDATE statements to get the individual counts for each column for each user.(Just a note on the actual database in case it's relevant: It has about 1000 LEVEL1 users, 50 Level2, and 15 Level 3 users. It will generate about 1400 to-do items per week.)Thanks in advance! This forum has been a great resource in the past!