I would to find the node that has the highest number of attributes - cypher

i am new in cypher. imagine that each person node defines by name and id, has some skills which are defined by id and vary from : skill1, skill2, skill3, skill4, skill5, skills6). I would like to return the person that has the highest number of skills. note that the same person id can have more that id in the skill node.
my query does not work can someone help me please
match (n:person)-[:HAS]->(f:skills)
with n,f
unwind keys(f)as key
with n,f, count(key) as p
with n, max(p) as s
where count(key)=s
return distinct n

Related

How to select in SQL

I have 2 tables I am working with, one is movie_score which contains an id, name, and score. I have another table that is movie_cast which contains mid, cid, and name. Mid is movie id and cid is cast id. the problem I must do is as follows:
Find top 10 (distinct) cast members who have the highest average movie scores. The output list must be sorted by score (from high to low), and then, by cast name in alphabetical order, if/when they have the same average score. The search must NOT include: (a) movies with scores lower than 50 AND (b) cast members who have appeared in less than 3 movies (again, only counting the number of appearances in movies with scores of at least 50). (Expected Output: cid, cname, average score)
I have tried to put the command together but so far this is all I was able to get:
SELECT DISTINCT movie_cast.cid, movie_cast.cname, FROM movie_score INNER JOIN movie_cast ON movie_score.id=movie_cast.mid ORDER BY cname LIMIT 10;
movie-name-score.txt goes with movie_score:
Example of .txt file
9,"Star Wars: Episode III - Revenge of the Sith 3D",80
24214,"The Chronicles of Narnia: The Lion, The Witch and The Wardrobe",76
1789,"War of the Worlds",74
10009,"Star Wars: Episode II - Attack of the Clones 3D",67
771238285,"Warm Bodies",-1
770785616,"World War Z",-1
771303871,"War Witch",89
771323601,"War of the Worlds the True Story",-1
movie-cast.txt goes with movie_cast:
Example:
9,162652153,"Hayden Christensen"
9,162652152,"Ewan McGregor"
9,418638213,"Kenny Baker"
9,548155708,"Graeme Blundell"
9,358317901,"Jeremy Bulloch"
9,178810494,"Anthony Daniels"
9,770726713,"Oliver Ford Davies"
9,162652156,"Samuel L. Jackson"
9,162655731,"James Earl Jones"
I expect to have an output something like:
162655731,"James Earl Jones",average score of the movies they have been in
Does anyone know the best way to create this command?

SQL query for selecting all items except given name and all rows with id of that given name

I apologize for the messy title. Please consider following tables:
CAR_MODEL : car_model_id, car_name
CAR_INVENTORY : car_model_id, car_location_name,
The user would pass in a car_location_name, and I would like to get a list of all car_name EXCLUDING rows with given car_location_name, and all cars with the id of that car_location_name.
Let me explain further.
For a join as such, let's assume that the user passes in "Germany." Then I would like to get a list excluding row #2 and #6, which have car_location_name of "Germany." I would also like to exclude any rows with the car_id of row with Germany. (In this case car_id of 2 and 6, so any row with car_id of 2 or 6 should be eliminated.)
In this case, since Germany has car_id of 2, I would like to get rid of the row with car_location_name of "Canada", since it also has car_id of 2.
The result should be:
What sql query (Can be sql server specific) can I use to achieve this?
I'm sorry if the explanation is confusing - please ask questions if you are having trouble understanding what I'm trying to say.
Simplest is probably to do the join to get the results as usual, and then just eliminate all car_model_ids that exist in Germany;
SELECT cm.car_model_id, ci.car_location_name, cm.car_name
FROM CAR_MODEL cm
JOIN CAR_INVENTORY ci
ON cm.car_model_id=ci.car_model_id
WHERE cm.car_model_id NOT IN (
SELECT car_model_id FROM CAR_INVENTORY WHERE car_location_name='Germany'
)
An SQLfiddle to test with.

sql query to show all results with foreign key

I have 2 primary tables
Semesters (Sem_id(PK),Sem_name)
Modules (Mod_id(PK),Mod_Name,Unit_id)
And a table called result_summary that keeps foreign keys from semesters and modules
(Res_sum_id(PK),Sem_id(FK),Mod_id(FK))
Final table Semester_Results enters the grades for a student in a particular module which took place in a particular semester
(Sum_res_id(PK),Res_sum_id(FK),Student_id(FK),Grade_id(FK))
and I write the below query
select m.Mod_id,m.Mod_Name, s.semester_name,g.Grade_id
from exa_modules m, exa_grades g, exa_semesters s, exa_results_summary rs,
exa_semester_results sr
where m.Mod_id=rs.Mod_id
and rs.Res_sum_id=sr.Res_sum_id
and s.Sem_id=rs.Sem_id
and sr.grade_id=g.grade_id
and student_id=3
group by rs.semester_id
The above query gives a result but does not display more than 1 module result in one semester.
For eg. Student 1 gave two modules in same semester but get only one module result
Can anyone please help?
Thanks in advance
You need to group by the semester AND the module the student took classes for... Otherwise it takes only the first one from all records based on one semester.
I managed to get other rows with the same query. However, (I have also posted a new question regarding this) the results are showing with separate rows in separate view.
Here is the query I used:
select m.unit_id,m.module_name,g.grade_short, s.semester_name
from exa_modules m, exa_grades g, exa_semesters s, exa_results_summary rs, exa_semester_results sr
where m.module_id=rs.module_id
and rs.result_sum_id=sr.result_sum_id
and s.semester_id=rs.semester_id
and sr.grade_id=g.grade_id
and user_id=3
group by rs.result_sum_id, rs.semester_id
order by s.semester_id

SQL movie database querying

I'm having trouble solving the following SQL requests:
Give the names of the actors that have acted in more films than 'sara allgood' and who have acted in films that won the 'cannes film festival'. Also, give the filmname.
Get the percentage of movies who won awards out of all movies produced between the years 1970 and 1990.
There are several tables but I'm assuming that only 4 are needed:
'films','remakes','casts', 'awtypes'
'films' attributes: filmid, filmname, year, director, studio, award
'remakes' attributes: filmid, title, year, priorfilm, prioryear
'casts' attributes: filmid, filmname, actor, award(10)
'awtypes' attributes: award(10), org(100), country, colloquial(50), year
It's a bit unclear to me how to match the award to the 'Cannes film festival' in the first query since the award field is only 10 characters meaning it is a reference to the awtypes table but I don't know which field in the awtypes table contains the name of the award and I don't have access to the database at the moment so it's either org or colloquial.
As for the second I don't know how I could compute the percentage but it seems that it should be solved using a union operator for the movies produced between 1970 and 1990 and the films that have won an award (I don't know how to place a condition for having at least one award).
A few hints, I hope they help you!
Give the names of the actors that have acted in more films than 'sara
allgood' and who have acted in films that won the 'cannes film
festival'. Also, give the filmname.
Based on the attributes you're stating, I would say that you can get to the right awtypes attribute via the casts table. They both contain the award(10) column. Given your data, I would expect the org(100) column to contain something on the organization that provides the prizes, so that would be my guess in this case for the cannes film festival content. But you would have to try it out and see what results you get. Unfortunately, as in this case, it is often quite hard to guess the contents of a column based only on column names.
Get the percentage of movies who won awards out of all movies produced
between the years 1970 and 1990.
Based on the info stated in your question, I would go with a guess that the award column in the films table contains a boolean or something that states if the movie won an award or not. You'd have to try this out. If that's the case, you can use a COUNT(*) on all movies between 1970 and 1990 and a COUNT(*) on all movies WHERE award = 1 (or something) to get the total numbers.
You could indeed combine these in a computation query with a UNION. Example that might help you:
SELECT SUM(cnt1) / SUM(cnt2) ... do the right computation here ...
FROM ( SELECT COUNT(*) AS cnt1
,0 AS cnt2
FROM table1
UNION ALL
SELECT 0 AS cnt1
,COUNT(*) AS cnt2
FROM table2) AS sub

Help with SQL aggregate functions

I've been learning SQL for about a day now and I've run into a road bump. Please help me with the following questions:
STUDENT (**StudentNumber**, StudentName, TutorialNumber)
TUTORIAL (**TutorialNumber**, Day, Time, Room, TutorInCharge)
ASSESSMENT (**AssessmentNumber**, AssessmentTitle, MarkOutOf)
MARK (**AssessmentNumber**, **StudentNumber**, RawMark)
PK and FK are identified within "**". I need to generate queries that:
1) List of assessment tasks results showing: Assessment Number, Assessment Title, and average Raw Mark. I know how to use the avg function for a single column, but to display something for multiple columns... a little unsure here.
My attempt:
SELECT RawMark, AssessmentNumber, AsessmentTitle
FROM MARK, ASSESSMENT
WHERE RawMark = (SELECT (RawMark) FROM MARK)
AND MARK.AssessmentNumber = ASSESSMENT.AssessmentNumber;
2) Report on tutorial enrollment showing: Tutorial Number, Day, Room, Tutor in Charge and number of students enrolled. Same as the avg function, now for the count function. Would this require 2 queries?
3) List each student's Raw Mark in each of the assessment tasks showing: Assessment Number, Assessment Title, Student Number, Student Name, Raw Mark, Tutor in Charge and Time. Sort on Tutor in Charge, Day and Time.
Here is an example for the first one, just take the logic and see if you can expand it to the other questions. I find that these things can be hard to lear if you can't find any solid examples but once you get the hang of it you'll sort it out pretty quick.
1)
SELECT a.AssessmentNumber, a.AssessmentTitle, AVG(RawMark)
FROM ASSESSMENT a LEFT JOIN MARK m ON a.AssessmentNumber = m.AssessmentNumber
GROUP BY a.AssessmentNumber, a.AssessmentTitle
OR not using a left join or alias table names
SELECT ASSESSMENT.AssessmentNumber, ASSESSMENT.AssessmentTitle, AVG(RawMark)
FROM ASSESSMENT,MARK
WHERE ASSESSMENT.AssessmentNumber = MARK.AssessmentNumber
GROUP BY ASSESSMENT.AssessmentNumber, ASSESSMENT.AssessmentTitle