As a beginner in SQL, JSON, and Fusion Table, I am looking to extract data from a Fusion Table and store it in a JavaScript variable, allowing me to display the data within a div element.
This is the progress of my JavaScript code so far:
var TopCity;
TopCity = '{
"dataSourceUrl": 'http://www.google.com/fusiontables/gvizdata?tq=',
"query":
'SELECT Location FROM 131fgSFd-cumxvMzICckXO-W4CldzfO9J9D--Vw9V ORDER BY Total_Task_Num DESC LIMIT 1',
}';
$("#TopCityDiv").append("<div>" + TopCity + "</div>");
The aim is to identify the city with the highest number of completed tasks and showcase its name.
Although I have gone through the Fusion Table SQL documentation, there are still aspects that confuse me: https://developers.google.com/fusiontables/docs/v1/sql-reference
Your assistance is greatly appreciated.
Update: Success has been achieved!
To execute queries on my fusion table rather than just save them, I must activate the Fusion Table API and acquire a public API key.
The operative code is as follows:
$(document).ready(function() {
var TopCity;
$.ajax({
type: "GET",
url: "https://www.googleapis.com/fusiontables/v1/query?sql=SELECT+Location+FROM+131fgSFd-cumxvMzICckXO-W4CldzfO9J9D--Vw9V+ORDER+BY+Total_Task_Num+DESC+LIMIT+1&hdrs=false&typed=false&fields=rows&key={API Key}",
success: function(data) {
TopCity = data["rows"];
$("#TopCityDiv").append("<div>" + TopCity + "</div>");
},
error: function(xhr, error) {
console.log('NaN');
}
});
});