Fusion Tables filter conditions OR - google-maps

According to https://developers.google.com/fusiontables/docs/developers_reference OR operations are not allowed as part of filter conditions. So I'm trying to come up with a creative way to solve the following:
I have a fusion table backed google map with hundreds of places and want to filter it to only places that have 'tags' or a 'title' containing a search parameter.
Ideally I could just use the following as my filter condition:
tags CONTAINS IGNORING CASE 'searchterm' OR title CONTAINS IGNORING CASE 'searchterm'
But the fusion table API simply doesn't allow it. So what to do? Make 2 separate queries, then filter out the duplicates? That means I can't use the nice FusionTablesLayer pre-rendered tile functionality.
What would you do?

A possible answer is to pre-render the data within the table. Essentially add another column which is an aggregate of tags and title. Then I only need to query the one 'tags_or_titles' column. Of course this means more data munging beforehand when I export the data into the fusion table and doesn't feel so nice and clean...

How about adding a column to your table called "Show_In_Results",
then running two separate queries to update that column for each row of data based on whether the search term is found in the specific column or not.
UPDATE 'table_id'
SET Show_In_Results = 1
UPDATE 'table_id'
SET Show_In_Results = 1
WHERE tags CONTAINS IGNORING CASE 'searchterm'
UPDATE 'table_id'
SET Show_In_Results = 1
WHERE title CONTAINS IGNORING CASE 'searchterm' and Show_In_Results <> 1
Then when you render your map layer:
SELECT 'columns' FROM 'table_id' WHERE Show_In_Results = 1

Related

Rails, MySql, JSON column which stores array of UUIDs - Need to do exact match

I have a model called lists, which has a column called item_ids. item_ids is a JSON column (MySQL) and the column contains array of UUIDs, each referring to one item.
Now when someone creates a new list, I need to search whether there is an existing list with same set of UUIDs, and I want to do this search using query itself for faster response. Also use ActiveRecord querying as much as possible.
How do i achieve this?
item_ids = ["11E85378-CFE8-39F8-89DC-7086913CFD4B", "11E85354-304C-0664-9E81-0A281BE2CA42"]
v = List.new(item_ids: item_ids)
v.save!
Now, how do I check whether a list exists which has item ids exactly matches with that mentioned in query ? Following wont work.
list_count = List.where(item_ids: item_ids).count
Edit 1
List.where("JSON_CONTAINS(item_ids, ?) ", item_ids.to_json).count
This statement works, but it counts even if only one of the item matches. Looking for exact number of items.
Edit 2
List.where("JSON_CONTAINS( item_ids, ?) and JSON_LENGTH(item_ids) = ?", item_ids.to_json, item_ids.size).count
Looks like this is working
You can implement a has many relation between lists and items and then access like this.
List.includes(:item).where('items.id in (?)',item_ids)
To implement has_many relation:
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

MySQL Select Data from multiple tables based on one id

I have a database with 2 tables that look like this:
content
id name
1 Cool Stuff
2 Even Better stuff
--
contentFields
id content label value
5 1 Rating Spectacular
6 1 Info Top Notch
7 2 Rating Poor
As you can see the content column of the contentFields table coincides with the id column of the content table.
I want to write a query that grabs all of the content and stores the applicable content fields with the right content, so that it comes out to this:
[
{
id: 1,
name: 'Cool Stuff',
contentFields: [
{label: 'Rating', value: 'Spectacular'},
{label: 'Info', value: 'Top Notch'}
]
},
{
id: 2,
name: 'Even Better Stuff',
contentFields: [
{label: 'Rating', value: 'Poor'}
]
}
]
I tried an inner join like this:
SELECT * FROM content INNER JOIN contentFields ON content.id = contentFields.content GROUP BY content.id
But that didn't do it.
*Note: I know that I could do this with 2 seperate queries, but I want to find out how to do it in one as that will dramatically improve performance.
What you are trying to achieve is not directly possible with SQL only.
As you have already stated yourself, you are looking for a table within a table. But MySQL does not know about such concepts, and as far as I know, other databases also don't. A result set is always like a table; every row of the result set has the same structure.
So either you let your GROUP BY content.id in place; then, for every row in the result set, MySQL will select a random row from the joined table which fits to that row's content.id (you even can't rely on that it is the same row every time).
Or you remove the GROUP BY; then you will get every row from the joined table, but that is not what you want as well.
When performance is an issue, I would probably choose the second option, adding ORDER BY content.id, and generate the JSON myself. You could do so by looping through the result set and begin a new JSON block every time the content.id changes.
Disclaimer The following is pure speculation.
I don't know anything about node.js and how it transforms result sets into JSON. But I strongly assume that you can configure its behavior; otherwise, it actually would not be of any use in most cases. So there must be a method to tell it how it should group the rows from a result set.
If I am right, you would first have to tell node.js how to group the result set and then let it process the rows from the second option above (i.e. without the GROUP BY).

Reshape the dataset into more relational format (Transpose SOME rows and assign them to a data subset)

I have a spreadsheet/csv:
Code:,101,Course Description:,"Introduction to Rocket Science",
Student Name,Lecture Hours,Labs Hours,Test Score,Status
John Galt,48,120,4.7,Passed
James Taggart,50,120,4.9,Passed
...
I need to reshape it to the following view:
Code:,Course Description:,Students,Lecture Hours,Labs Hours,Average Test Score,Teaching Staff
101,"Introduction to Rocket Science",John Galt,48,120,4.7,Passed
101,"Introduction to Rocket Science",James Taggart,50,120,4.9,Passed
...
Beleive it or not, can not get the right idea how to do that despite it seems to be very primitive transformation, is there any silver bullet for this?
Original records (csv) have in a way json-like structure so my first approach was to represent the original data as a vector and then transpose it, (but in this case my resulting table looks like sparced matrix - rows I have transpored are blank in the rest of its values)
Another way Im considering - **serialize it into jsons and then de-serialize** into new spreadsheet (jsonize()) - in this case, Im having problems with merging them properly.
In both ways I have it "half-working";
Can anyone suggest simple and reliable algorithm for this;
Any language, RegEx, any tools, code snippets are very appreciated
Assuming that the pattern you've described here is consistent throughout, there are quite a few different approaches you could take I think, but in all cases you basically can use that fact that the 'Course' rows start with "Code:" but that's never going to be a student name.
You can take advantage of this either by a regular expression find/replace, or within OpenRefine.
Example:
Open file in a text editor that supports regular expressions in
find/replace
Search for lines starting with 'Code:' and add additional commas to the start of the row to shift the course data columns to the
right e.g. search for: ^Code: replace with: ,,,,,^Code:
If you now import the file into OpenRefine then you'll have a project with 10 columns (the 10th col is caused by the trailing
comma at the end of the course data row)
You can now use Transpose (or just rename) on the right-most columns which contain the course data, while leaving the left-most
columns which contain the student details
Isolate the rows that contain the phrase 'Student Name' in the first column and remove them (via a filter or facet)
Move the Course Code/Description columns to the beginning of the project, and use the 'Edit Cells->Fill Down' option on each column to get the values repeated on all the relevant lines
Finally rename the columns as you want, remove any extraneous columns

Rows count of Couchbase view subset

When I query some view in Couchbase I get the response that has following structure:
{
"total_rows":100,
"rows":[...]
}
'total_rows' is very useful property that I can use for paging.
But lets say I select only a subset of view using 'start_key' and 'end_key' and of course I don't know how big this subset will be. 'total_rows' is still the same number (as I understand it's just total of whole view). Is there any easy way to know how many rows was selected in subset?
You can use the in-built reduce function _count to get the total count of your query.
Just add _count as reduce function for your view. After that, you will need to make two calls to couchbase:
In one call, you'll set the query param reduce=true (along with either group=true or group_level=n, depending upon how you're sending your key(s)). This will give you the total count of your filtered rows.
In the other call, you'll disable the reduce function with reduce=false because you now need the actual rows.
You can find more details about map and reduce at http://docs.couchbase.com/admin/admin/Views/views-writing.html
You can just use an array count/total/length in whatever language you are using.
For example in PHP:
$result = $cb->view("dev_beer", "beer_by_name", array('startkey' => 'O', 'endkey'=>'P'));
echo "total = >>".count($result["rows"])
If you're actually wanting to paginate your data then you should use limit and skip:
http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writing-querying-pagination.html
If you have to paginate the view in the efficient way, you actually don't need to specify both start and the end.
Generally it is possible to use startkey/startkey_id and limit. In this case the limit will tell you that the page won't be bigger than known size.
Both cases are described in CouchDB book: http://guide.couchdb.org/draft/recipes.html#pagination
Here is how it works:
Request rows_per_page + 1 rows from the view
Display rows_per_page rows, store + 1 row as next_startkey and next_startkey_docid
As page information, keep startkey and next_startkey
Use the next_* values to create the next link, and use the others to create the previous link

MySQL modify values of one column based on contents of another

Basically I want to query a database and modify the values of one column based on the contents of another.
Here's my idea of how it would work:
IF Column 'Town' IS NOT NULL then Column 'Sign-up type' = 1 else = 0
The logic is, i've added a new column into the DB that will store whether a quick or full sign up has been made.
Quick = 0, Full = 1. Default is 0 = Quick.
I've managed to implement the change on the two registration forms that feed the DB, but I need to append the historical data to backwards fill the data.
Because the quick sign up only collects name, and email, those entries do not contain data in the 'Town' field which is a required field in a full sign up.
So i'm using that as a reference point to select all the entries that DO have (NOT NULL?) data in order to enter '1' (representing 'Full') into 'Signup Type' column.
I hope I'm making sense! I only have a basic understanding of MySQL but I'm willing to learn, it's sometimes hard trying to explain what I want to do when I'm unclear of the correct jargon!!
UPDATE yourTable SET signupType = IF(Town IS NULL, 1, 0);
Note this will update all data, you may want to limit this to historical data (by the sounds of things this should be fine however).