• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

SQLite Query - Sorting (Android app)

clamum

Lifer
I have a SQL query for an Android app I've been working on that I can't seem to get the ordering right for. Here it is:

Code:
SELECT DISTINCT strftime('%Y', [date]) AS [year],
	strftime('%m', [date]) AS [month],
	firearmId AS [_id],
	COUNT([st].[firearmId]) AS [numDates]
FROM [shot_track] AS [st]
WHERE [st].[deleted] = '0'
	AND [st].[firearmId] = '1'
GROUP BY [year], [month]
ORDER BY [year], [month] DESC
The "shot_track" table has a "date" column that I store dates in, with "YYYY-MM-DD" format (per sqlite specs). What I want to do is order the results by "year", with newest first, and then by "month" (also newest first).

I don't quite understand why this query isn't doing that. The screenshot below is the results of the query with the data I have in the table now:

IJOgOmo.png


Any idea why the ordering isn't correct? If I take the "month" off the "ORDER BY" clause, the ordering is correct, but I want the months also ordered descending, after years.
 
I'd expect to need to add DESC to each field that I'm sorting by (where I want descending) , but I've never used SQLite.

Does (year) DESC, (month) DESC work?
 
Seriously? I don't know why I figured they'd be both ordered descending with the way I did it.

Yeah, that definitely worked Dave. Thanks much. 🙂
 
Back
Top