

When to Use the COUNT() FunctionĬOUNT() is one of SQL’s most common aggregate functions. In summary, GROUP BY is used to organize and summarize data based on specific columns, functions or expressions, which will allow you to gain insights and perform calculations on distinct groups within a dataset.

There's no error handling, and I'd suggest using a DB Utilities class that extends SQLiteOpenHelper, but this should get you going.If you’re looking for an in-depth review of basic SQL concepts like COUNT() and GROUP BY, I recommend our interactive SQL Basics course. Do something here with lat, lng, count SQLiteDatabase db = SQLiteDatabase.create(null) ĭb = SQLiteDatabase.openDatabase("/data/data//databases/myDB.db", null, SQLiteDatabase.OPEN_READONLY, null) Ĭursor cursor = db.rawQuery("SELECT lat, lng, COUNT(*) count FROM tasks GROUP BY lat, lng HAVING count > 1 ", new String )
Sqlite count group by example code#
If you're having a problem with the execution of the SQL using SQLiteDatabase.query(), then post a code sample with the output (or failure) so that the problem can be diagnosed.Īs for code in Android, here's an example that works using rawQuery. To address that, and get all rows, remove the HAVING count > 1, yielding lat lng count which is consistent with what you expect, except for "and for others i should get count as 1". INSERT INTO tasks (lat, lng, id) VALUES (11, 35, 412) Īnd selecting with the query specified (note column names match table in data example) SELECT lat, lng, COUNT(*) count FROM tasks INSERT INTO tasks (lat, lng, id) VALUES (12, 35, 147) INSERT INTO tasks (lat, lng, id) VALUES (12, 34, 143) INSERT INTO tasks (lat, lng, id) VALUES (12, 34, 123)
