Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sengoku
Starting Member
29 Posts |
Posted - 2005-02-02 : 16:24:13
|
| hey there peeps,i have a small optimization problem i was wondering if anyone knew how to deal with...i have a list of forums, with forum_au as the autokey and forum_name as the namethen i have a list of threads in the forums.this looks like :thread_authread_titlethread_lastpost_datethread_lastpost_authorthread_lastpost_idthread_viewcountnow, at the moment i doselect * from forums order by forum_nameand then within a loop in asp i do select top 1 * from threads where thread_forum = XXX order by thread_lastpost_date descthat works ok, but isn't probably that effecient.now, this is my problem :from 1 query, can i get a list of all forums, with a sum of all 'thread_viewcount's, as well as the id number, date, and author of the latest post in each?like soforum_auforum_namethread_lastpost_idthread_lastpost_authorthread_lastpost_datetimesum(thread_viewcount)?thanking you in advancechris |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-02-02 : 16:42:28
|
| By joining the 2 tables and using a GROUP BY You will be able to do this.this shows the concept:select a,b,c,max(x),sum(y)from t1 join t2 on t1.key = t2.fkeygroup by a,b,crockmoose |
 |
|
|
sengoku
Starting Member
29 Posts |
Posted - 2005-02-02 : 16:49:28
|
quote: Originally posted by rockmoose By joining the 2 tables and using a GROUP BY You will be able to do this.this shows the concept:select a,b,c,max(x),sum(y)from t1 join t2 on t1.key = t2.fkeygroup by a,b,c
but if i doselect forum_au,forum_name,thread_au,thread_title,thread_lastpost_author,thread_lastpost_id,MAX(thread_lastpost_date),SUM(thread_viewcount)from forums f join threads t on t.thread_forum=f.forum_augroup by forum_au,forum_name,thread_au,thread_title,thread_lastpost_author,thread_lastpost_idthe group by causes it to return mutiple rows for each forum, since every thread will have a unique thread_au, but i want to return the thread_au of the one with the MAX(thread_lastpost_date) only...but still sum ALL of the viewcounts for that forum upi know i could do something likeselect forum_au,forum_name,sum(thread_viewcount) from forums join threads on thread_forum=forum_au group by forum_au,forum_nameto get the sum alone, but can i get the lastpost details too with 1 clever query? :) |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-02-02 : 16:59:41
|
Yeah, its a little wacky. I think you need this:(I'm assuming you've got forum_au in your thread table)(and I didn't add any data to the table vars)declare @forum Table (forum_au int identity ,forum_name varchar(50))declare @thread Table (forum_au int ,thread_au int identity ,thread_title varchar(50) ,thread_lastpost_date datetime ,thread_lastpost_author varchar(50) ,thread_lastpost_id int ,thread_viewcount int)Select f.forum_au ,f.forum_name ,t.thread_lastpost_id ,t.thread_lastpost_author ,t.thread_lastpost_date ,t.sumViewCountFrom @forum f--Left join to get all forums with or without threadsLeft JOIN (--get all thread data for most recent post Select t1.forum_au ,t1.thread_lastpost_id ,t1.thread_lastpost_author ,t1.thread_lastpost_date ,agg.sumViewCount From @thread t1 JOIN (--get the most recent post Select forum_au ,max(thread_lastpost_date) maxDate ,count(*) sumViewCount From @thread group by forum_au ) as agg ON t1.forum_au = agg.forum_au AND t1.thread_lastpost_date = maxDate ) as t ON f.forum_au = t.forum_au Be One with the OptimizerTG |
 |
|
|
sengoku
Starting Member
29 Posts |
Posted - 2005-02-02 : 17:05:59
|
| nice one, thanks TG :)i knew someone would have a crazy solution like that :D |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-02-02 : 17:17:10
|
| Too bad you can't just right click this page, view source, and get all of Graz's cool Topic/Thread Sql Code :)Be One with the OptimizerTG |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-02-03 : 04:44:47
|
Nice TG.Wasn't reading the specs very carefully.This is with correlated subqueries:SELECT f.forum_au ,f.forum_name ,thread_lastpost_id = ( SELECT t.thread_lastpost_id FROM thread t WHERE t.forum_au = f.forum_au AND t.thread_lastpost_date = last_post.last_date ) ,thread_lastpost_author = ( SELECT t.thread_lastpost_author FROM thread t WHERE t.forum_au = f.forum_au AND t.thread_lastpost_date = last_post.last_date ) ,sum_viewcount = ( SELECT SUM(t.thread_viewcount) FROM thread t WHERE t.forum_au = f.forum_au )FROM forum f LEFT JOIN( SELECT forum_au, MAX(thread_lastpost_date) AS last_date FROM thread GROUP BY forum_au ) last_post ON f.forum_au = last_post.forum_au rockmoose |
 |
|
|
|
|
|
|
|