Monday, March 19, 2012

Advanced sort

I need some help writing this sql query.
I have three columns in my table
let call them
tOrderA,tOrderB,tContent
tOrderA,tOrderB are random integers from 1-10 000, tContent is a text field
so my table looks something like this
|tOrderA|tOrderB|tContent|
1 27 my
9 3 asfdf
152 16 sdfsd
18 22 dfdf
182 14 dfdsfs
tOrderA is my primary sort with levels (ie.1-100,101-200,201-300)
tOrderB is my secondary sort.
when the sort is applied the table should look like this.
|tOrderA|tOrderB|tContent|
9 3 asfdf
18 22 dfdf
1 27 my
182 14 dfdsfs
152 16 sdfsd
How would I write this query?
select tContent from tableA order by tOrderA ASC [range
1-100,101-200,201-300...] and tOrderB ASC
Thanks,
Aaronselect tOrderA, tOrderB, tContent
from tableA
order by (tOrderA + 1) / 100, tOrderB
"Aaron" wrote:

> I need some help writing this sql query.
> I have three columns in my table
> let call them
> tOrderA,tOrderB,tContent
> tOrderA,tOrderB are random integers from 1-10 000, tContent is a text fiel
d
> so my table looks something like this
> |tOrderA|tOrderB|tContent|
> 1 27 my
> 9 3 asfdf
> 152 16 sdfsd
> 18 22 dfdf
> 182 14 dfdsfs
>
> tOrderA is my primary sort with levels (ie.1-100,101-200,201-300)
> tOrderB is my secondary sort.
> when the sort is applied the table should look like this.
> |tOrderA|tOrderB|tContent|
> 9 3 asfdf
> 18 22 dfdf
> 1 27 my
> 182 14 dfdsfs
> 152 16 sdfsd
> How would I write this query?
> select tContent from tableA order by tOrderA ASC [range
> 1-100,101-200,201-300...] and tOrderB ASC
>
> Thanks,
> Aaron
>
>|||You mean:
select tOrderA, tOrderB, tContent
from tableA
order by (tOrderA - 1) / 100, tOrderB
^^^^^
With your code 99 and 100 will be in the [101 - 200] bracket.
Jacco Schalkwijk
SQL Server MVP
"CBretana" <cbretana@.areteIndNOSPAM.com> wrote in message
news:B77956D1-7821-4B24-B0A2-E898FA3B3F48@.microsoft.com...
> select tOrderA, tOrderB, tContent
> from tableA
> order by (tOrderA + 1) / 100, tOrderB
> "Aaron" wrote:
>|||Right ! Thanks! would that be the infamous "off by 2" error '
"Jacco Schalkwijk" wrote:

> You mean:
> select tOrderA, tOrderB, tContent
> from tableA
> order by (tOrderA - 1) / 100, tOrderB
> ^^^^^
> With your code 99 and 100 will be in the [101 - 200] bracket.
> --
> Jacco Schalkwijk
> SQL Server MVP
>
> "CBretana" <cbretana@.areteIndNOSPAM.com> wrote in message
> news:B77956D1-7821-4B24-B0A2-E898FA3B3F48@.microsoft.com...
>
>

No comments:

Post a Comment