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 2005 Forums
 Transact-SQL (2005)
 Most efficient mode

Author  Topic 

jdblack
Starting Member

11 Posts

Posted - 2011-01-05 : 12:30:58
How would you guys go about making a mode function? Custom CLR aggregate? I'm making one in C# right now. Is there a way to ensure values are ordered by SQL before being passed into the aggregate function?

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-01-05 : 12:35:38
mode as in modal average?
I would probably use a ranking function.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jdblack
Starting Member

11 Posts

Posted - 2011-01-05 : 12:37:53
Yep, modal average. I'm trying to come up with a super efficient method because the aggregate will be ranking datasets every couple minutes, each with several million records minimum.
Go to Top of Page

jdblack
Starting Member

11 Posts

Posted - 2011-01-05 : 12:44:05
Is there a way to ensure SQL sorts the value before ordering them? Or, is there a way to iterate through a custom aggregate more than once (so that I can sort before aggregating)? I'm not very familiar with custom aggregates & the examples I've found online assume one iteration through all values.
Go to Top of Page

jdblack
Starting Member

11 Posts

Posted - 2011-01-05 : 12:45:38
Never mind, I suppose I can sort it while it is looping through the accumulations, then find the largest occurrence(s) during terminate.
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-01-05 : 12:47:52
I would say that you need to persist the counts and index them. It then becomes a simple select top 1 order by count desc.

Another option

select top 1 *
from
(
select fld, rnk=count(*)
from tbl
group by fld
) a
order by rnk desc

or maybe
select top 1 fld, rnk=count(*)
group by fld
order by rnk desc


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -