Microsoft SQL Server (MSSQL) 'order by' to keep values together, but without specifying order (partial order by) -


i using mssql 2008 r2 general sql question. want sort results keep same values next each other, without specifying exact sort order.

for example

create table t (a int not null, b int not null)  insert t values (1, 1), (1, 2), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2)  select * t order 

that have property want in rows a=1 appear first, a=2, a=3. equally specify 'order b desc' , a=3 rows first.

but in fact, happy a=2 rows, a=1, a=3.

so query above over-specifying; asking server particular sort order when don't want order; want same values grouped together. large table, server might able query more efficiently if has more flexibility choose order return rows, subject requirement same values together.

is there sql construct such as

   select *    t    order indeterminate 

where specify 'any ordering like, long equal elements stay together'?

i don't think there's describe, clustered index on field return rows in order of indexed value without order by, can't guaranteed.

however, if had index cost of order a trivial.

and of course if wanted randomize order that, seemed hoping better performing option, , method not perform better.


Comments