0

I'm having this following weird issue with Sqlite query in Android 4.4.2 version devices.Thanks in advance

The table structure is as below

CREATE TABLE responsemaster (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    formid integer,
    proposerid text,
    fieldid integer,
    response text,
    remarks text,
    listId integer,
    isMarked integer
)

and the query that giving error is

SELECT id as id, formid as formid, proposerid as proposerid, MIN(fieldid) fieldid, response as response, remarks as remarks, listId as listId, isMarked as isMarked
FROM responsemaster
WHERE formid = 40066 AND proposerid = '7ca6533a-c5f0-43e2-9980-83f9ae2c7370201802230113550000'
GROUP BY fieldid

The exception i'm getting is as follows

android.database.sqlite.SQLiteException: aggregate functions are not allowed in the GROUP BY clause (code 1):, 
while compiling: SELECT id as id, formid as formid, proposerid as proposerid,  **MIN(CAST(fieldid AS INTEGER)) AS fieldid**, response as response, remarks as remarks, listId as listId, isMarked as isMarked FROM responsemaster WHERE formid = 40066 AND proposerid = '7ca6533a-c5f0-43e2-9980-83f9ae2c7370201802230113550000'  GROUP BY fieldid

The above query will execute successfully in SQLite browser and in Greater than Android 4.4.2 version devices..Please can any one help me how i can able to fix that error?

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
Shiv
  • 53
  • 1
  • 11
  • 1
    Perhaps support for that was added in SQLite 3.8, as Android 4.4 and older [use older versions of SQLite](https://stackoverflow.com/a/4377116/115145). – CommonsWare Feb 23 '18 at 12:13

2 Answers2

0

try this query it may replace the aggregate function

SELECT id as id, formid as formid, proposerid as proposerid, fieldid as 
       fieldid, response as response, remarks as remarks, listId as listId, 
       isMarked as isMarked FROM responsemaster WHERE formid = 40066 AND proposerid = '7ca6533a-c5f0-43e2-9980-83f9ae2c7370201802230113550000' 
       GROUP BY fieldid HAVING fieldid = MIN(fieldid)
Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28
0

Use HAVING clause instead of WHERE with AGGREGATE Functions :) It will solve it

https://learn.microsoft.com/en-us/sql/ssms/visual-db-tools/use-having-and-where-clauses-in-the-same-query-visual-database-tools

Zainab Jamil
  • 143
  • 10