Question

There is a table named images in mediastore database in android, the images table has a column named timetaken which represents created time of the image, and it is in milliseconds unit.

Now I have a demand that I want to group the images by created day(not the time), and count images of each day, so I have try this query:

SELECT datetaken/86400000*86400000 as dateT, count(dateT) as imageSumInDate FROM images WHERE (1 = 1 ) group by (dateT) ORDER BY dateT DESC

Unfortunately, it comes out a error:

android.database.sqlite.SQLiteException: no such column: dateT (code 1)

Then I ask someone else for the problem, and he give a sub-query style like this below:

select dateT,COUNT(dateT) as imageSumInDate from (SELECT datetaken/86400000*86400000 as dateT FROM images)t WHERE (1 = 1 ) group by (dateT) ORDER BY dateT DESC

This may be correct, but I can't use this sub-query in ContentResolver's query method. ContentResolver may only support the query like this style:

select ____ from table where ______ order by____

Is there any trick to help me fit the ContentResolver's query style?

Any help would be really appreciated!

Was it helpful?

Solution

Try this:

SELECT datetaken/86400000*86400000 as dateT, count(datetaken) as imageSumInDate FROM images WHERE (1 = 1 ) GROUP BY (datetaken/86400000*86400000) ORDER BY datetaken/86400000*86400000 DESC

The issue is that the dateT column alias is not valid in the scope of the ORDER BY clause. Simply repeating the dateT expression rather than using the dateT alias should solve the problem.

EDIT: Removed all references to dateT column alias from query. (Which clearly I should have done to start. :)

OTHER TIPS

It is also solved by a trick to fit the ContentResolver's query style:

select dateT,COUNT(dateT) as imageSumInDate from (SELECT datetaken/86400000*86400000 as dateT FROM images where (1=1))t group by (dateT) ORDER BY dateT DESC

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top