forum

Home / DeveloperSection / Forums / SQL Server pick random (or first) value with aggregation

SQL Server pick random (or first) value with aggregation

Anonymous User215907-May-2013
Hi Everyone!

How can I get SQL Server to return the first value (any one, I don't care, it just needs to be fast) it comes across when aggregating?

For example, let's say I have:

ID      Group
1       A
2       A
3       A
4       B
5       B
and I need to get any one of the ID's for each group. I can do this as follows:

Select 
max(id)
,group 
from Table 
group by group
which returns

ID      Group
3       A
5       B
That does the job, but it seems stupid to me to ask SQL Server to calculate the highest ID when all it really needs to do is to pick the first ID it comes across.

Thanks

PS - the fields are indexed, so maybe it doesn't really make a difference?


Updated on 07-May-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By