Friday, March 30, 2012

How to make the order by date fine enough in SQL

hi,

I was pulling up a report in SQL, and I wanted the records to be ordered by dates descending. However, I found this ordering was only fine enough to order records by dates (not hours or minutes) (within the same date, records were ordered so that the latest entered were at the bottom). I wonder if anyone else has encouted this problem before, or I am doing something wrong.

Thanks very much.

Then you're trying to do a select order by datedesc, and group by date, within each group you wanna order by timeasc, right? If so you’re trying to do sorting within grouped records. In SQL2005 you can useRANK Functions, for example:

SELECT*,RANK()OVER(PARTITIONBYCONVERT(VARCHAR,CDATE,102)

ORDERBYCONVERT(VARCHAR,CDATE,108))AS RANK

FROM testP

ORDERBYCONVERT(VARCHAR,CDATE,102)DESC

|||Thanks!Big Smile

No comments:

Post a Comment