Tommy Naidich
Tommy Naidich

Reputation: 762

An SQL statement

I'm trying to come up with a working SQL query statement to serve my needs but I'm having a hard time with that.

I've got a table which consists users. There are several fields within that table, two of them are 'score' and 'date'. When the user reaches a certain score, I add the timestamp of the current date to the 'date' field. Otherwise, that field remains empty.

I'm trying to sort that list in order to get those who've got the newest date first, then the rest of the users sorted by their score. For instance:

user1 - score:500 - 2/JAN/13
user2 - score 409 - NULL
user3 - score 500 - 3/JAN/13
user4 - score 500 - 1/JAN/13
user5 - score 239 - NULL

After executing my query, I'd like to have the next output:

user4
user1
user3
user2
user5

I hope I made myself clear. This is what I've got so far:

SELECT userName FROM tblUsers ORDER BY date,score ASC

The problem with this code, as much as I have managed to understand, is that it sorts the list by the date and score fields in ascending order, however after sorting the date ascendingly I need the score descendingly.

Any ideas?

Upvotes: 0

Views: 50

Answers (1)

Mark PM
Mark PM

Reputation: 2919

Trythis:

SELECT userName FROM tblUsers ORDER BY date ASC,score DESC

Upvotes: 3

Related Questions