I want to create a query to a ContentResolver that will return all the call logs (name, number, photo) between a specified time, but it needs to be sorted by the number of counts.
A MySQL query would look like:
SELECT count(*) as counts, CallLog.Calls.CACHED_NAME,
CallLog.Calls.NUMBER, CallLog.Calls.CACHED_PHOTO_ID
FROM calls
WHERE CallLog.Calls.DATE > '2016-02-02' AND CallLog.Calls.DATE < '2016-02-04'
GROUP BY CallLog.Calls.CACHED_NAME order by counts DESC;
This is how I made the query, but I need also the group by and the order:
Cursor cur = .getContentResolver()
.query(callUri, null, CallLog.Calls.DATE + " BETWEEN ? AND ?", whereValue, null);
Can you please help me to add group by name and order by the count of each item?
Related
i want to write a query to get last message send or receive to each contact. but unfortunalty group by clause is creating some issue like by writing the following query i get the last message that is send to a contact and if no message send to a contact then the last receive message is return by this query.
String where = "type=? OR type=? ) GROUP BY ( thread_id ";
String[] whereArgs = new String[] { "" + Constants.SMS_RECEIVE,
"" + Constants.SMS_SENT };
try {
Cursor cursor = ctx.getContentResolver().query(Constants.URI_SMS,
smsProjection, where, whereArgs, "date DESC");
and if i remove the group by clause i get all the messages send or receive to a contact.
String where = "type=? OR type=? ";
String[] whereArgs = new String[] { "" + Constants.SMS_RECEIVE,
"" + Constants.SMS_SENT };
try {
Cursor cursor = ctx.getContentResolver().query(Constants.URI_SMS,
smsProjection, where, whereArgs, "date DESC");
the resone of using group by clause is that why fetch un related records from database.
When you do GROUP BY you can get surprising results for fields, it's really most useful for aggregate functions like SUM() or COUNT(). Depending on how you need to handle multiple contacts, you could ORDER BY (some date field) DESC and then use LIMIT to get just the single most recent entry.
I want to retrieve all the records in ascending order with a WHERE clause. But where to include the WHERE condition in my query? It is working fine for ascending order.
Here is my code. First I am working on this query but here I am not able to implement the ORDER BY clause.
Cursor tripdaycursor = sdb.rawQuery(
"select TripDay_Id, Tripday_Date, Tripday_ParsingDate,
Trip_Complete, TripDay_Endkm, TripDay_Count, Tripday_EndPlace
FROM TripDay WHERE Trip_Id="+record, null);
So I tried the code below for ascending order:
String recordid=Integer.toString(record), Trip_Id="Trip_Id", TripDay_Id="TripDay_Id",
Tripday_Date="Tripday_Date", Tripday_ParsingDate="Tripday_ParsingDate",
Trip_Complete="Trip_Complete", TripDay_Endkm="TripDay_Endkm",
TripDay_Count="TripDay_Count", Tripday_EndPlace="Tripday_EndPlace",
TripDay = "TripDay";
Cursor tripdaycursor = sdb.query(TripDay, new String[] {
TripDay_Id, Tripday_Date, Tripday_ParsingDate, Trip_Complete, TripDay_Endkm,
TripDay_Count, Tripday_EndPlace}, null, null, null, null,
Tripday_ParsingDate + " ASC");
But I want both conditions on a single query, with the WHERE clause and in ascending order. How to do that?
You can put "order by Column_Name" in your query after the where clause. Something like this, with the column you want to order by in ascending order
Cursor tripdaycursor = sdb.rawQuery("select TripDay_Id,Tripday_Date,Tripday_ParsingDate,Trip_Complete,TripDay_Endkm,TripDay_Count,Tripday_EndPlace from TripDay where Trip_Id="+record + " order by " + columnName,null);
I am using the query method of SQLiteDatabase. I need help with the orderBy parameter of this method.
Cursor cursor = sqLiteDatabase.query(tableName, tableColumns, whereClause, whereArgs, groupBy, having, orderBy);
public Cursor getAllCustomexp(int TID) throws SQLException
{
Cursor mCursor =
db.query(false, CEXP_TABLE, new String[] {KEY_CEID, FLD_CETID, FLD_CEEID, FLD_CEMID,
FLD_CEAMT, FLD_CESEL}, FLD_CETID + " = " + TID, null,
null, null, FLD_CEEID, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Question 1:
In the above query the result set will be sorted by FLD_CEEID in ascending or descending order ?
Question 2:
If I need to order the result set first by FLD_CEEID and then by FLD_CEMID how should i construct the order by parameter of this query.
Is it possible to do multiple order by using this method?
From SQLite docs:
If a SELECT statement that returns more than one row does not have an
ORDER BY clause, the order in which the rows are returned is
undefined. Or, if a SELECT statement does have an ORDER BY clause,
then the list of expressions attached to the ORDER BY determine the
order in which rows are returned to the user. Rows are first sorted
based on the results of evaluating the left-most expression in the
ORDER BY list, then ties are broken by evaluating the second left-most
expression and so on. The order in which two rows for which all ORDER
BY expressions evaluate to equal values are returned is undefined.
Each ORDER BY expression may be optionally followed by one of the
keywords ASC (smaller values are returned first) or DESC (larger
values are returned first). If neither ASC or DESC are specified, rows
are sorted in ascending (smaller values first) order by default.
Answer 1: Result set will be sorted in ascending order.
Answer 2:
String orderBy = FLD_CEEID + " ASC, " + FLD_CEMID + " ASC";
db.query(false, CEXP_TABLE, new String[] {KEY_CEID, FLD_CETID, FLD_CEEID, FLD_CEMID,
FLD_CEAMT, FLD_CESEL}, FLD_CETID + " = " + TID, null,
null, null, orderBy, null);
It works like in SQL Select Order By. You can find details here: http://en.wikipedia.org/wiki/Order_by_(SQL) .
I am currently working on an Android project in Eclipse and i am having problems with my SQL query.
I am trying to order the query by more than two columns, currently i am doing it by KEY_DAY_ID but i want to also do it by KEY_START_TIME, but i can't get it to work
my query currently looks like this:
Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE,
KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
null, null, null, null, KEY_DAY_ID + " ASC");
Please let me know your thoughts. Thank you in advance!
The last parameter in db.query() method is the order by clause (without the "order by"). All you need to do is separate both columns by a ",". So it would look like:
Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE,
KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
null, null, null, null, KEY_DAY_ID + " ASC, " + KEY_START_TIME + " ASC");
This works for me
SQLiteCursor cursor = (SQLiteCursor) db.query(DbHelper.TIMES, colmn, null, null, null, null, DbHelper.TABLE_DAY + " ASC, " + DbHelper.TABLE_LECTURE_NO + " ASC",null);
Also you can do it in select line like this:
Cursor data = ddbb.rawQuery("SELECT * FROM vacations ORDER BY NAME ,MONTH , date ",null);
in previous code the first probability for the first column "NAME" then will start arrange by the Second probability "MONTH" then the third "date".....
which mean working in series
Or:
Cursor data = ddbb.rawQuery("select * from vacations where NAME = ? ORDER BY MONTH AND date ",new String[]{ns});
in previous code by using "AND" the two conditions are working together in parallel
I created a data base table using SQLite in my project, I want to retrieve records from the database for the last week, for the last month, for the last year, when user click on the specified Buttons. But I don't know how to retrieve the records. Is there any function exist to get these records accordingly ?
You'll need to construct a "where" clause like so:
ContentResolver cr = getContentResolver();
Cursor groupCur = cr.query(
Groups.CONTENT_URI, // what table/content
new String [] {Groups._ID, Groups.NAME}, // what columns
"Groups.NAME NOT LIKE + 'System Group:%'", // where clause(s)
null, // ???
Groups.NAME + " ASC" // sort order
);
I spaced this out so it's easier to explain, but this is what you typically see as:
Cursor groupCur = cr.query(Groups.CONTENT_URI, null, null, null, null);
I found the query
SELECT * FROM DATABASE_TABLE
WHERE strftime('%Y-%m-%d',DATE) >= date('now','-6 days') AND
strftime('%Y-%m-%d',DATE)<=date('now') order by DATE