I have three columns -
TheDate
- Obviously a date.TheID
- A strictly increasing ID.TheType
- A Record t
Taking the liberty granted in comments to assume that TheDate
is non-decreasing with ascending TheId
, if r1.TheId < r2.TheId
then it must be the case that r1.TheDate <= r2.TheDate
(that's the definition of non-decreasing). In that case, ordering first by TheDate
and then by TheId
produces the same order as ordering by TheId
alone. Looking at it from the other direction, ordering by TheId
automatically produces results clustered by TheDate
and in order by date.
But what you're already doing differs from ordering by (TheDate
, TheId
) (which we already established is the same as ordering by just TheId
) only by moving the special records to the end of each date cluster, which is exactly what you say you want. Thus, you must be getting your results in the desired order; if you in fact have any problem then it must be that you are dissatisfied with the means by which you are doing so. For instance, perhaps you are concerned with query performance.
If your existing ordering indeed produces the correct results, however, then I'm having trouble seeing an alternative that I would expect to deliver substantially better performance. No doubt the ordering can be produced by means that do not rely on TheDate
to be non-decreasing, but I would expect all of those to be comparatively expensive to compute.