SQLTeam.com | Weblogs | Forums

An "order by" statement I can't figure out


#1

Hi

I have a table with 2 columns (among others)- ID, and Title.
I want to order the table so that the rows are grouped together by Title, but they are ordered by the minimal ID for each group of Titles.

Example:

Title, ID

cat, 7
cat, 9
dog, 11
dog, 13
dog, 17
bird, 12
...

etc. I'm guessing I need some kind of "group by" by min(Id)?

Thanks alot


#2
ORDER BY
	MIN(ID) OVER (PARTITION BY Title),
	ID

#3

I'm probably being thick ...

Some reason you can't do

ORDER BY Title, ID

?


#4

OK, old-brain now caught up ...

"Bird" comes after "Cat" and "Dog" because its lowest ID is higher than the lowest ID of the others.


#5

Thanks, that worked. I wasn't even familiar with that partition thing.