0

In Android I am having a table which stores latitude and longitude values, along with a corresponding id that is associated with every row. when i am using the query

SELECT latitude, longitude, COUNT(*) count
FROM tasks
GROUP BY latitude, longitude
HAVING count > 1

It is giving me group by latitude only and not longitude. how can I achieve this? I want to get the count of tasks with unique lat long values only. is there any way to achieve this using Android SQLite? For example, with this data in the table...

lat   lng   id
12    34    123
12    34    143
12    35    147
11    35    412

... for 12 lat 34 lng I must get 123, 143 i.e. count as 2 and for others I should get count as 1

CJBS
  • 15,147
  • 6
  • 86
  • 135
  • `i want to get the count of tasks with unique lat long values only` --> If you want to get the count of tasks, why are you selecting latitude and longitude values as well? – iRuth Feb 25 '15 at 06:26
  • at same latitude and longitude i may be more than one tasks located. so i want to get unique lat lng and have corresponding taskids with it. – change_is_necessity Feb 25 '15 at 06:30
  • @Android_Rocks I've updated my answer to include a code sample. http://stackoverflow.com/a/28713682/3063884 – CJBS Feb 25 '15 at 17:14

2 Answers2

2

Recreating the scenario...

CREATE TABLE tasks 
    (
     lat integer, 
     lng integer, 
     id  integer
    );

INSERT INTO  tasks (lat, lng, id) VALUES (12, 34, 123);
INSERT INTO  tasks (lat, lng, id) VALUES (12, 34, 143);
INSERT INTO  tasks (lat, lng, id) VALUES (12, 35, 147);
INSERT INTO  tasks (lat, lng, id) VALUES (11, 35, 412);

And selecting with the query specified (note column names match table in data example)

SELECT lat, lng, COUNT(*) count FROM tasks
GROUP BY lat, lng HAVING count > 1

Gives the following:

lat   lng   count
12    34    2

...which is consistent with what you expect, except for "and for others i should get count as 1". To address that, and get all rows, remove the HAVING count > 1, yielding

lat   lng   count
11    35    1
12    34    2
12    35    1

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.

Fiddling with this query

As for code in Android, here's an example that works using rawQuery. This seems to support the HAVING clause.

SQLiteDatabase db = SQLiteDatabase.create(null);

db = SQLiteDatabase.openDatabase("/data/data/com.mycompany.myapp/databases/myDB.db", null,    SQLiteDatabase.OPEN_READONLY, null);

Cursor cursor = db.rawQuery("SELECT lat, lng, COUNT(*) count  FROM tasks GROUP BY lat, lng  HAVING  count  > 1 ", new String [] {});

// Iterate through cursor
if(cursor.moveToFirst())
{
    do
    {
        Integer lat = cursor.getInt(0);
        Integer lng = cursor.getInt(1);
        Integer count = cursor.getInt(2);

        // Do something here with lat, lng, count

    }
    while(cursor.moveToNext());
}

There's no error handling, and I'd suggest using a DB Utilities class that extends SQLiteOpenHelper, but this should get you going.

CJBS
  • 15,147
  • 6
  • 86
  • 135
1

It looks like a problem in your SQL. When you create an Alias for a column, you probably should use the "AS" keyword.

I think your SQL statement should be:

SELECT latitude, longitude, COUNT(*) AS count FROM tasks GROUP BY latitude, longitude HAVING count > 1

or

SELECT latitude, longitude, COUNT(*) FROM tasks GROUP BY latitude, longitude HAVING COUNT(*) > 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • there query is working fine in sqlite browser but in android code it is not – change_is_necessity Feb 25 '15 at 06:47
  • @Android_Rocks When you say it works in the browser, but not in code, what do you mean? When I run the query, it gives one row (as you expect). What's the error (or different result) you're getting in Android, and what code are you using to execute it? – CJBS Feb 25 '15 at 06:53
  • I was trying to say that you are not "naming" the column properly - http://stackoverflow.com/questions/6744803/sqlite-count-group-and-order-by-count – Jim Feb 25 '15 at 06:56
  • @Jim "AS" seems to be optional according to the documentation - https://www.sqlite.org/syntax/result-column.html – CJBS Feb 25 '15 at 07:04
  • i want to use this in the query code. like database.query(n args ). as given in this documentation. http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#query%28boolean,%20java.lang.String,%20java.lang.String[],%20java.lang.String,%20java.lang.String[],%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String%29 – change_is_necessity Feb 25 '15 at 07:18
  • @CJBS I have run into problems not using "as" - I believe in particular when using "GROUP BY" - aggregating, grouping and restricting based on "HAVING" in the same query is tricky. I will gladly withdrawn my response if it is not helpful. – Jim Feb 25 '15 at 07:32
  • @Jim You had a go at attempting to answer another's question, which is what this is all about. Perhaps next time, though you could highlight in your answer the change you're proposing. I had to copy/paste from the OP's query and compare against yours to see what was different. – CJBS Feb 25 '15 at 07:37
  • 1
    @CJBS thanks for your feedback. I thought it was clear enough, but I agree that it wasn't now that you pointed it out. In the spirit of your comments, I hope this helps the OP. I edited my response. – Jim Feb 25 '15 at 07:40