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 |
|
FR33D0M_F1GHTR
Starting Member
3 Posts |
Posted - 2008-03-28 : 16:40:45
|
| Ok, I have a table with two columns in the group by clause, ServerID and Database ID. I have 9 unique ServierIDs and 117 unique DatabaseIDs. I need a query that says gimme the 5 highest database id hitcount totals for each server id. I would like to do this without a #temp table or a cursor.here's what I have:-------------------------------------------------------------------create table #server_hitcount_summary(SID int not null,DBID int not null,ORD int not null,SUM_HITCOUNT bigint not null,SUM_MAXWRITES bigint not null,SUM_MAXREADS bigint not null,SUM_MAX_DURATION bigint not null,SUM_MAX_CPU bigint not null)declare @id intset @id = 1while (@id < 20)begininsert #server_hitcount_summaryselect top (@top)SID, DBID, ROW_NUMBER() over (order by sum(hitcount) desc) as ORD, sum(hitcount) as SUM_HITCOUNT, sum(MAX_WRITES) as SUM_MAXWRITES, sum(MAX_READS) as SUM_MAXREADS, sum(MAX_DURATION) as SUM_MAX_DURATION,max(max_cpu) as MAX_MAX_CPUfrom hitcounts_table group by SID, DBID having SID = @id and DBID <> 1 order by sum_HITCOUNT descset @id = @id + 1endselect SID, DBID, ORD, SUM_HITCOUNT,SUM_MAXWRITES, SUM_MAXREADS ,SUM_MAX_DURATION,SUM_MAX_CPU from #server_hitcount_summary order by SID ascdrop table #server_hitcount_summary-------------------------------------------------------------------What d'ya think? is there a better way to do this?-The Universe is naturally cold. It takes the power of a star to change that. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-03-29 : 02:07:37
|
Hope this is what you're looking for:-SELECT * FROM(SELECT ROW_NUMBER() OVER(PARTITION BY t.SID GROUP BY SUM_HITCOUNT DESC) AS RowNo,*FROM(select SID, DBID, sum(hitcount) as SUM_HITCOUNT, sum(MAX_WRITES) as SUM_MAXWRITES, sum(MAX_READS) as SUM_MAXREADS, sum(MAX_DURATION) as SUM_MAX_DURATION,max(max_cpu) as MAX_MAX_CPUfrom hitcounts_table group by SID, DBID having SID = @id and DBID <> 1 )t)t1WHERE t1.RowNo<=5 |
 |
|
|
FR33D0M_F1GHTR
Starting Member
3 Posts |
Posted - 2008-03-31 : 11:45:44
|
| thanks. That works.-The Universe is naturally cold. It takes the power of a star to change that. |
 |
|
|
|
|
|
|
|