Neo4J : How to Order results by attributes value - sorting

How do I order my results using Node properties value using Cypher Queries?
For Example :
I have the below results :
Person
City
Kasper
London
Sean
Chicago
Tom
London
Kate
New York
John
London
Sarah
Chicago
I want to sort the above result on basis of "London" i.e people from London should be on top. The sorted results can look something like this :
Person
City
Kasper
London
John
London
Tom
London
Kate
New York
Sean
Chicago
Sarah
Chicago
I know Order By clause can be used to sort in ascending or descending order but can we use it to sort on the basis of Value? If not, please suggest some other way to do it. Thanks in advance.

you can sort by property value . imagine (p:Person) and (c:City) nodes
RETURN p.name AS Person, c.name AS City
ORDER BY City
another example
WITH p,c
ORDER BY p.birthdate
RETURN p.name AS Person, c.name AS City
in case you want to order by age.
if your cities have to be in an order based on a specific property, add e.g. index to the city node , and do something like:
WITH p,c
ORDER BY c.index
RETURN p.name AS Person, c.name AS City

Related

Is this natural join operation used correctly? (Relational Algebra)

I have the following task given from the professor:R-E Modell
Assume the companies may be located in several cities. Find all companies located in every city in which “Small Bank Corporation” is
located.
Now the professor's solution is the following:
s ← Π city (σ company_name=’Small Bank Corporation’ (company))
temp1 ← Π comp_id, company_name (company)
temp2 ← Π comp_id, company_name ((temp1 × s) − company)
result ← Π company_name (temp1 − temp2)
I for myself found a completely different solutions with a natural join operation which seems much simpler:
What I tried to do was using the natural joint operation which whe defined as following that a relation r and s are joined on their common attributes. So I tried to get all city names by using a projection on a selection of all companies with the company_name "Small Bank Cooperation". After that I joined the table with the city names with the company table, so that I get all company entrys which have the city names in it.
company ⋈ Π city (σ company_name=”Small Bank Cooperation” (company)))
My question now is if my solution is also valid, since it seems a little bit to trivial?
Yours isn't the same.
My answer here says how to query relationally. It uses a version of the relational algebra where headings are sets of attribute names. My answer here summarizes it:
Every query expression has an associated (characteristic)
predicate--statement template parameterized by attributes. The tuples
that make the predicate into a true proposition--statement--are in
the relation.
We are given the predicates for expressions that are relation names.
Let query expression E have predicate e. Then:
R ⨝ S has predicate r and s
R ∪ S has predicate r or s
R - S has predicate r and not s
σ p (R) has predicate r and p
π A (R) has predicate exists non-A attributes of R [r]
When we want the tuples satisfying a certain predicate we find a way
to express that predicate in terms of relation operator
transformations of given relation predicates. The corresponding query
returns/calculates the tuples.
Your solution
company ⋈ Π city (σ company_name=”Small Bank Corporation” (company)))
is rows where
company company_id named company_name is in city
AND FOR SOME company_id & company_name [
company company_id named company_name is in city
AND company_name=”Small Bank Corporation”]
ie
company company_id named company_name is in city
AND FOR SOME company_id [
company company_id named ”Small Bank Corporation” is in city]
ie
company company_id named company_name is in city
AND some company named ”Small Bank Corporation” is in city
You are returning rows that have more columns than just company_name. But your companies are not the requested companies.
Projecting your rows on company_name gives rows where
some company named company_name is in some city
AND some company named ”Small Bank Corporation” is in that city
After that I joined the table with the city names with the company
table, so that I get all company entrys which have the city names in
it.
That isn't clear about what you get. However the companies in your rows are those in at least one of the SBC cities. The request was for those in all of the SBC cities:
companies located in every city in which “Small Bank Corporation” is located
The links I gave tell you how to compose queries but also convert between query result specifications & relational algebra expressions returning a result.
When you see a query for rows matching "every" or "all" of some other rows you can expect that that part of your query involves relational-division or some related idiom. The exact algebra depends on what is intended by the--frequently poorly/ambiguously expressed--requirements. Eg whether "companies located in every city in which" is supposed to be no companies (division) or all companies (related idiom) when there are no such cities. (The normal mathematical interpretation of your assignment is the latter.) Eg whether they want companies in exactly all such cities or at least all such cities.
(It helps to avoid "all" & "every" after "find" & "return", where it is redundant anyway.)
Database Relational Algebra: How to find actors who have played in ALL movies produced by “Universal Studios”?
How to understand u=r÷s, the division operator, in relational algebra?
How to find all pizzerias that serve every pizza eaten by people over 30?

How can I do a UNION operation with DexieJS?

I have a indexedDB with firstName and lastName as columns.
Here is some sample data:
firstName lastName
__________________
John Doe
Mary Doe
Jacob Adams
Doe Mary
When I search for Doe, the query should return the first two entries and the last. If I search for Jacob, it should return the second last.
I followed examples from this Dexie sample, but I can't do a single search on two fields at the same time. If not, is there a way to run two questions and UNION the results to a single one?
Sounds like this:
table.where('firstName').equals('Doe').or('lastName').equals('Doe')

How to query distinct column value when query all row data

I hava one MySQL Table
id name birthdate city
1 Owen 2011/01/01 USA
2 Mark 2012/05/01 UK
3 Marry 2011/01/01 JP
4 John 2011/01/01 JP
First,I uesd jqgrid to read all row data. But Now,I want to know when birthdate=2011/01/01,how many different city in the table.
Can don't used sql,only used jqgrid plugin?
You are looking for distinct function.
SELECT DISTINCT(city) FROM table WHERE birthday = "2011/01/01";

oracle how to get intersect of this two

select DISTINCT country, count(river)
from geo_river
group by country
having count(river) > 3
INTERSECT
select DISTINCT country, count(province)
from geo_lake
group by lake,country
having count(province) > 3;
I'm sure USA is in it, but the result shows nothing, where is wrong?
I can't add pictures.
river_geo has river, country, province.
lake_geo has lake, country, province
"have lakes next to more than three provinces." means the lake has more than 3 province but these province belongs to the same country.
I want to find country that have this kind of lakes and have more than 3 rivers

Where two or more values match condition?

I have been asked this question;
You list county names and the surnames of the representatives if the representatives in the counties have the same surname.
and I have the following tables;
***REPRESENTATIVE***
REPI SURNAME FIRSTNAME COUNTY CONS
---- ---------- ---------- ---------- ----
R100 Gorege Larry kent CON1
R101 shneebly john kent CON2
R102 shneebly steve kent CON3
I cant seem to figure out the correct way to ask Orical to display a surname that exists more then twice and the surnames are in the same country.
I know how to ask WHERE something = something, but that's doesn't ask what I want to know.
It sounds like you want to use the HAVING clause after doing a GROUP BY
SELECT surname, county, count(*)
FROM you_table
GROUP BY surname, county
HAVING count(*) > 1;
If you really mean "more than twice" as you wrote, none of the data you'd want HAVING count(*) > 2 but then none of your sample data would be returned.
In words, this SQL statement says
Group the data into buckets by surname and county. Each distinct combination of surname and county is a separate bucket.
Count the number of rows in each bucket
Return those buckets where there are at least two rows

Resources