How Can I write a group by query to a fusion table?

199 Views Asked by At

I want to get all the coordinates in a event(having eventId) group them according to the user_id then Order each coordinate set according to time in descending order and take the top.(the last updated coordinate).. I tried this query.

var query3 = "https://www.googleapis.com/fusiontables/v2/query?sql=SELECT user_id,Location,Time FROM "+tableID+" WHERE event_id = 23"+" GROUP BY user_id ORDER BY Time DESC LIMIT 1"+"&key="+apiKey;

It gave me this error :

{
 "error": {
  "errors": [
   {
    "domain": "fusiontables",
    "reason": "badQueryCouldNotParse",
    "message": "Invalid query: (col2, LOCATION) must appear in group-by when aggregates  are present: []",
    "locationType": "parameter",
    "location": "q"
   }
  ],
  "code": 400,
  "message": "Invalid query: (col2, LOCATION) must appear in group-by when aggregates  are present: []"
 }
}
1

There are 1 best solutions below

0
On

There can be more than one location - time combination for a user_id...

You have 2 possibilities:

  1. to GROUP BY user_id, Location, Time , or
  2. to use an aggregate like this:
    SELECT user_id, min(Location), min(Time) FROM ... GROUP BY user_id