javascriptsqlgoogle-maps-api-3google-fusion-tables

Trouble with a complicated SQL query for Google Fusion Table. Can I use external values within my query?


The basic outline of this project is it's a visual representation of journey time between London Wards.

So far I've set up a google fusion table which responds well to basic queries. But now I'm trying to change the color of my fusion table polygons according to the journey time to/from a chosen polygon.

function fillcolour(match) {
  var limit = 0;
  var filter = [];

  for (x in match) {
    if (limit < 5) {
      var duration = match[x];
      if (dataraw[1]) {
          //-------75 and 65 are stand in values at the moment. They will later be filled by variables.
          filter.push("(" + generateWhere(x, duration, 75, 65) + ")");
      }
      limit++;
    }
  }
  where = filter.join(' OR ');

  PolygonLayer.setOptions({
    query: {
      select: 'shape',
      from: mapTable
    },
    styles: [{
      polygonOptions: {
        fillColor: "#000FFF",
        fillOpacity: .8
      }
    },
    {
      where: where,
      polygonOptions: {
        fillColor: "#FFF000"
      }
    }],
  })
};

function generateWhere(Keys, duration, high, low) {
  var whereClause = [];
  whereClause.push("name");
  whereClause.push(" = '");
  whereClause.push(Keys);
  whereClause.push("' AND ");
  whereClause.push(duration);
  whereClause.push(" >= ");
  whereClause.push(low);
  whereClause.push(" AND ");
  whereClause.push(duration);
  whereClause.push(" < ");
  whereClause.push(high);
  return whereClause.join('');
}

I understand there's probably a more simple way to deal with the polygon options but currently my SQL query is not behaving how I'd like it to.

This is the query that's being fed into the where statement:

(name = 'Abbey Wood' AND 80 >= 65 AND 80 < 75) OR (name = 'Acton' AND 74 >= 65 AND 74 < 75) OR (name = 'Anerley' AND 46 >= 65 AND 46 < 75)

Does anyone have any idea why this wouldn't be working?


Solution

  • (name = 'Abbey Wood' AND 80 >= 65 AND 80 < 75) OR (name = 'Acton' AND 74 >= 65 AND 74 < 75) OR (name = 'Anerley' AND 46 >= 65 AND 46 < 75)

    There are two problems with the query

    1) Fusion Table Queries:

    "You must use quotes around any column names in the select or where fields that contain spaces, reserved words, or that do not begin with a letter".

    2) Row and Query SQL Reference:

    "OR is not supported."

    So you need to add quotes and split your OR subqueries into separate queries

    styles: [{
        polygonOptions: {
            fillColor: "#000FFF",
            fillOpacity: .8
        }
    },
    {
        where: "name = 'Abbey Wood' AND '80' >= 65 AND '80' < 75",
        polygonOptions: {
            fillColor: "#FFF000"   
        }
    },
    {
        where: "name = 'Acton' AND '74' >= 65 AND '74' < 75",
        polygonOptions: {
            fillColor: "#FFF000"    
        }
    },
    {
        where: "name = 'Anerley' AND '46' >= 65 AND '46' < 75",
        polygonOptions: {
            fillColor: "#FFF000"    
        }
    }]