I am learning the bigquery with the example: personsData
https://developers.google.com/bigquery/docs/data#nested
in this example, there is a repeated filed called children. Is this possible to make a query to find out who have child called "Josh" and child call "Jim"
I tried the following query but no result return:
select fullName from [xxx.xx] where children.name = "Jane" and children.name = "John"
A couple of things are wrong with the above query.
There should be an or not an and between the two criteria. e.g. children.name = "Josh" or children.name = "Jim"
If you are using the standard google data set then there is no children with the name "Josh" or "Jim"
try:
select fullName from PersonsData.personsdata where children.name = "Jane" or children.name = "John"
with google datasets
Related
I have 2 nodes:
Students and Subjects.
I want to be able to add multiple student names to multiple subjects at the same time using cypher query.
So far I have done it by iterating through the list of names of students and subjects and executing the query for each. but is there a way to do the same in the query itself?
This is the query I use for adding 1 student to 1 subject:
MATCH
(s:Student)-[:STUDENT_BELONGS_TO]->(c:Classroom),
(u:Subjects)-[:SUBJECTS_TAUGHT_IN]->(c:Classroom)
WHERE
s.id = ${"$"}studentId
AND c.id = ${"$"}classroomId
AND u.name = ${"$"}subjectNames
AND NOT (s)-[:IN_SUBJECT]->(u)
CREATE (s)-[:IN_SUBJECT]->(u)
So I want to be able to receive multiple subjectNames and studentIds at once to create these connections. Any guidance for multi relationships in cypher ?
I think what you are looking for is UNWIND. If you have an array as parameter to your query:
studentList :
[
studentId: "sid1", classroomId: "cid1", subjectNames: ['s1','s2'] },
studentId: "sid2", classroomId: "cid2", subjectNames: ['s1','s3'] },
...
]
You can UNWIND that parameter in the beginning of your query:
UNWIND $studentList as student
MATCH
(s:Student)-[:STUDENT_BELONGS_TO]->(c:Classroom),
(u:Subjects)-[:SUBJECTS_TAUGHT_IN]->(c:Classroom)
WHERE
s.id = student.studentId
AND c.id = student.classroomId
AND u.name = in student.subjectNames
AND NOT (s)-[:IN_SUBJECT]->(u)
CREATE (s)-[:IN_SUBJECT]->(u)
You probably need to use UNWIND.
I haven't tested the code, but something like this might work:
MATCH
(s:Student)-[:STUDENT_BELONGS_TO]->(c:Classroom),
(u:Subjects)-[:SUBJECTS_TAUGHT_IN]->(c:Classroom)
WITH
s AS student, COLLECT(u) AS subjects
UNWIND subjects AS subject
CREATE (student)-[:IN_SUBJECT]->(subject)
Using the Django ORM is it possible to perform a select_related (left join) with conditions additional to the default table1.id = table2.fk
Using the example models:
class Author(models.Model):
name = models.TextField()
age = models.IntegerField()
class Book(models.Model):
title = models.TextField()
and the raw sql
SELECT 'Book'.*, 'Author'.'name'
FROM 'Book'
LEFT JOIN
'Author'
ON 'Author'.'id' = 'Book'.'author_id'
AND 'Author'.'age' > 18 ;<---this line here is what id like to use via the ORM
I understand that in this simple example you can perform the filtering after the join, but that hasn't worked in my specific case. As i am doing sums across multiple left joins that require filters.
# gets all books which has author with age higher than 18
books = Book.objects.filter(author__age__gt=18)
returns queryset.
Then you can loop trough the queryset to access specific values and print them:
for b in books:
print(b.title, b.author.name, b.author.age)
Suppose I have 3 hypothetical models;
class State(models.Model):
name = models.CharField(max_length=20)
class Company(models.Model):
name = models.CharField(max_length=60)
state = models.ForeignField(State)
class Person(models.Model):
name = models.CharField(max_length=60)
state = models.ForeignField(State)
I want to be able to return results in a Django app, where the results, if using SQL directly, would be based on a query such as this:
SELECT a.name as 'personName',b.name as 'companyName', b.state as 'State'
FROM Person a, Company b
WHERE a.state=b.state
I have tried using the select_related() method as suggested here, but I don't think this is quite what I am after, since I am trying to join two tables that have a common foreign-key, but have no key-relationships amongst themselves.
Any suggestions?
Since a Person can have multiple Companys in the same state. It is not a good idea to do the JOIN at the database level. That would mean that the database will (likely) return the same Company multiple times, making the output quite large.
We can prefetch the related companies, with:
qs = Person.objects.select_related('state').prefetch_related('state__company')
Then we can query the Companys in the same state with:
for person in qs:
print(person.state.company_set.all())
You can use a Prefetch-object [Django-doc] to prefetch the list of related companies in an attribute of the Person, for example:
from django.db.models import Prefetch
qs = Person.objects.prefetch_related(
Prefetch('state__company', Company.objects.all(), to_attr='same_state_companies')
)
Then you can print the companies with:
for person in qs:
print(person.same_state_companies)
This is a multi question
From a mongoose result I have several items in the result which I get through result.length.
How can I get specific items e.g.
Person.find({'exployment.office':'Greenway'})
.exec(function(err, result){
//from the result, how can i get those items with
result.length would give me the number of items in the result
but how can i get a specific item without having to use loop to get
the specific item within the result
//There are like fifty people in the result,
//how can i get the number of items in result
});
In SQL, I have a query like this
select *
from table1, table2
where table2.field2 = value1
and table2.field2 = table1.field2
and table1.value1 = value3
e.g.
select *
from city, state
where state.name = 'xxy'
and state.id = city.state_id
and city.name != 'yyy'
How can this be converted to mongoose?
In SQL if I want to select people whose first name are e.g. Smith, Queen I can use things like
select *
from table
where first_name in (Smith, Queen)
This would give me result for people whose first_name matches SMith and Queen
How can I do this in mongoose?
select *
from Person
where first_name in (Smith, Queen)
Would simply be this using $in:
Person.find({'first_name': { $in: ['Smith', 'Queen']}}) ...
Next one:
select *
from city, state
where state.name = 'xxy'
and state.id = city.state_id
and city.name != 'yyy'
Using mongoose, you would need to use populate and create the schemas with ref relationships.
City.find({ "name": { $ne: "yyy"} }).populate({
"path": "state",
"match": { "state.name": "xxy" }
}) ...
Next one:
Person.find({'exployment.office':'Greenway'})
.exec(function(err, result){
//from the result, how can i get those items with
result.length would give me the number of items in the result
but how can i get a specific item without having to use loop to get
the specific item within the result
//There are like fifty people in the result,
//how can i get the number of items in result
});
You would filter as much as you can before you exec the query to get the records you need rather than filter after you get all in the result. result.length sure would give you the count although you could get the count via something like this;
Person.count({first_name: "Smith"}).exec() ...
I am looking for a perticular raw SQL query using Inner Join.
I have those models:
class EzMap(models.Model):
layers = models.ManyToManyField(Shapefile, verbose_name='Layers to display', null=True, blank=True)
class Shapefile(models.Model):
filename = models.CharField(max_length=255)
class Feature(models.Model):
shapefile = models.ForeignKey(Shapefile)
I would like to make a SQL Query valid with PostgreSQL that would be like this one:
select id from "table_feature" where' shapefile_ezmap_id = 1 ;
but I dont know how to use the INNER JOIN to filter features where the shapefile they belongs to are related to a particular ezmap object
Something like this:
try:
id = Feature.objects.get(shapefile__ezmap__id=1).id
except Feature.DoesNotExist:
id = 0 # or some other action when no result is found
You will need to use filter (instead of get) if you want to deal with multiple Feature results.