SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


ORDER BY [DATE] producing unordered result


ORDER BY [DATE] producing unordered result

Author
Message
jay.jose
jay.jose
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 31
Hi,

I'm having a problem with a query. I need to order a query result by the amount then by date. I have the following at the end of my query:

ORDER BY
AMOUNT DESC,
[DATE] ASC

the amount did order properly but the date is just messed up...here's a sample result i got
DATE AMOUNT
1992-01-31 00:00:00.000 20.00
1991-05-13 00:00:00.000 20.00
1991-03-14 00:00:00.000 20.00
1990-12-08 00:00:00.000 20.00
2007-12-27 00:00:00.000 20.00
1992-05-28 00:00:00.000 15.00
1992-04-18 00:00:00.000 15.00
1991-06-17 00:00:00.000 15.00
Michael Valentine Jones
Michael Valentine Jones
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5810 Visits: 11771
The date looks OK to me. Why do you think the date is "messed up".
jay.jose
jay.jose
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 31
Michael Valentine Jones (7/20/2009)
The date looks OK to me. Why do you think the date is "messed up".


DATE AMOUNT
1992-01-31 00:00:00.000 20.00
1991-05-13 00:00:00.000 20.00
1991-03-14 00:00:00.000 20.00
1990-12-08 00:00:00.000 20.00
2007-12-27 00:00:00.000 20.00 <--- this should be on top of the list since its dated 2007
1992-05-28 00:00:00.000 15.00
1992-04-18 00:00:00.000 15.00
1991-06-17 00:00:00.000 15.00
Michael Valentine Jones
Michael Valentine Jones
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5810 Visits: 11771
Do it this way to get the date in decending order within amount.
ORDER BY
AMOUNT DESC,
[DATE] DESC


jay.jose
jay.jose
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 31
here are other result i'm getting

ORDER BY
AMOUNT DESC,
[DATE] DESC

1991-12-17 00:00:00.000 15.00
1992-07-18 00:00:00.000 15.00
2001-04-17 00:00:00.000 15.00
2000-09-06 00:00:00.000 15.00 <-- from 1991, 1992, 2001 then back to 2000
1983-02-21 00:00:00.000 11.67
1981-10-24 00:00:00.000 11.67
1985-02-16 00:00:00.000 11.67
1982-06-23 00:00:00.000 11.67
1981-02-24 00:00:00.000 11.67
1980-06-27 00:00:00.000 11.67
1979-10-29 00:00:00.000 11.67
1979-03-01 00:00:00.000 11.67
1985-10-16 00:00:00.000 11.67
1986-06-15 00:00:00.000 11.63 <--- all the 11.67 are messed up also
Michael Valentine Jones
Michael Valentine Jones
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5810 Visits: 11771
What is the data type of the AMOUNT column?

Could it be a MONEY datatype that you are stripping the last 2 digits from to show with 2 decimal places?
jay.jose
jay.jose
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 31
Yup, it is a MONEY datatype.. Thanks for the input i'll look into it ^_^
GilaMonster
GilaMonster
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88300 Visits: 45277
Can you post the entire query?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


jay.jose
jay.jose
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 31
Hi,

I saw where the problem is..there are two [DATE] fields on the table (i guess 1 is the actual date and the second is the date when they encoded the transaction) and most of it has the same date but I noticed that there are some that have different values on it. And thats what has been causing the problem during sort. Thanks for all the time and help. I'll be posting more problems as they arise ^_^
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87290 Visits: 41113
jay.jose (7/21/2009)
I'll be posting more problems as they arise ^_^


I recommend you study the article at the first link in my signature lines below before you do that. You'll be amazed at how much it helps.;-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search