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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 probably very simple, but i just can't get it...

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 name
then i have a list of threads in the forums.

this looks like :
thread_au
thread_title
thread_lastpost_date
thread_lastpost_author
thread_lastpost_id
thread_viewcount

now, at the moment i do

select * from forums order by forum_name

and then within a loop in asp i do

select top 1 * from threads where thread_forum = XXX order by thread_lastpost_date desc

that 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 so

forum_au
forum_name
thread_lastpost_id
thread_lastpost_author
thread_lastpost_datetime
sum(thread_viewcount)

?

thanking you in advance

chris

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.fkey
group by a,b,c


rockmoose
Go to Top of Page

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.fkey
group by a,b,c



but if i do

select 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_au
group by forum_au,forum_name,thread_au,thread_title,thread_lastpost_author,thread_lastpost_id

the 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 up

i know i could do something like

select forum_au,forum_name,sum(thread_viewcount) from forums join threads on thread_forum=forum_au group by forum_au,forum_name

to get the sum alone, but can i get the lastpost details too with 1 clever query? :)
Go to Top of Page

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.sumViewCount
From @forum f
--Left join to get all forums with or without threads

Left 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 Optimizer
TG
Go to Top of Page

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
Go to Top of Page

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 Optimizer
TG
Go to Top of Page

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
Go to Top of Page
   

- Advertisement -