How can I query an edge property by value? - sql

I'm using OrientDB (Community-2.0.9) and have two vertices: Person and University and one edge: isStudent. The edge isStudent has the property 'mark' as float. Person --isStudent--> University.
Now I want to select all persons, where the mark is greater than 3.0, but I got no results, but if I query for equality I got two results.
Do you have any ideas how to solve this?
Queries:
SELECT FROM PERSON WHERE out_isStudent.mark = 3.4 --> two results
SELECT FROM PERSON WHERE out_isStudent.mark > 3.0 --> no results

If you:
select out_isStudent.mark from Person
you see that it returns the list [3.4] instead of 3.4
The query that should not work is the first since you're comparing [3.4] == 3.4
You can achieve what you want with:
select from Person where out_isStudent[0].mark > 3

Related

SQL: Calculate the rating based on different columns and use it as an argument

I'm trying to calculate the rating based on a table that has 3 columns with different ratings ranging from 1 to 5.
I wanted to calculate the average of these 3 values and then be able to use this as an argument in queries, for example:
Where Rating >3.5
At this moment I have this that gives me the average for all suppliers
SELECT c.Name
,(SELECT CAST(AVG(rat) AS DECIMAL(5, 2))
FROM(
VALUES(b.Qty_Price),
(b.Quality),
(b.DeliveryTime)) A (rat)) AS Rating
FROM Order a
JOIN Evaluation b ON b.ID_Evaluation = a.ID_Evaluation
JOIN Supplier c ON c.NIF_Supplier = a.NIF_Supplier
What I would like now is, for example, to filter the providers that have more than 3 ratings, but I don't know how I can do that. If anyone can help i would be grateful
If the query works like you want it, you get the average for all entries, that is.
The WHERE rating > 3.5 cannot be added, as rating does not exist in the context of the SELECT-clause, nor the tables we JOIN.
To overcome this issue, we can keep the query that you have made, call it something different using WITH and SELECT from that sub-query WHERE rating > 3.5
It should look something like this:
WITH Averages(name, rating) AS
(SELECT c.name
,(SELECT CAST(AVG(rat) AS DECIMAL(5, 2))
FROM(
VALUES(b.qty_Price),
(b.quality),
(b.deliveryTime)) AS (rat)) AS rating
FROM Order a
JOIN Evaluation b ON b.ID_Evaluation = a.ID_Evaluation
JOIN Supplier c ON c.NIF_Supplier = a.NIF_Supplier)
SELECT name, rating FROM Averages WHERE rating > 3.5;
Now, we simply call the query you provided as Averages for example, and we SELECT from that table WHERE rating > 3.5.
Also note that you can have multiple WITHs to make things easier for you, but remember that a comma (,) is needed to seperate them. In our case, we only have 1 use of WITH ... AS, so no need for a comma or semi-colon after ...= a.NIF_Supplier)
Looks like you typed only "A" before "(rat)", it should be "AS". Also, remember that attributes should be lowercase, it makes it easier for all of us to distinguish tables from attributes.
Cheers!

What is the simplest way in SQL to determine a ratio(1:1) from a generated result?

I'm very new to SQL and from what I've been told the solution to my query is a simple line or two of code. Basically, I have a database of individual profiles that includes name, favorite book, political leanings and others. I was provided with a code cell that determines percentages and lift - see below:
and result:
Basically I just want to narrow the results down to instances where the perc_con and perc_libs are within a range of .8-1.2:.8-1.2 i.e 1:1.
I hope that makes sense.
Thanks!
Your perc_con is a calculated field which is ( BC.C/C.NC). So your where will be
where BC.C > 5
and BL.L > 5
and ( BC.C/C.NC) > 0.8
and ( BC.C/C.NC) < 1.2
and ( BL.L/L.NL) > 0.8
and ( BL.L/L.NL) < 1.2

What do OrientDB's functions do when applied to the results of another function?

I am getting very strange behavior on 2.0-M2. Consider the following against the GratefulDeadConcerts database:
Query 1
SELECT name, in('written_by') AS wrote FROM V WHERE type='artist'
This query returns a list of artists and the songs each has written; a majority of the rows have at least one song.
Query 2
Now try:
SELECT name, count(in('written_by')) AS num_wrote FROM V WHERE type='artist'
On my system (OSX Yosemite; Orient 2.0-M2), I see just one row:
name num_wrote
---------------------------
Willie_Cobb 224
This seems wrong. But I tried to better understand. Perhaps the count() causes the in() to look at all written_by edges...
Query 3
SELECT name, in('written_by') FROM V WHERE type='artist' GROUP BY name
Produces results similar to the first query.
Query 4
Now try count()
SELECT name, count(in('written_by')) FROM V WHERE type='artist' GROUP BY name
Wrong path -- So try LET variables...
Query 5
SELECT name, $wblist, $wbcount FROM V
LET $wblist = in('written_by'),
$wbcount = count($wblist)
WHERE type='artist'
Produces seemingly meaningless results:
You can see that the $wblist and $wbcount columns are inconsistent with one another, and the $wbcount values don't show any obvious progression like a cumulative result.
Note that the strange behavior is not limited to count(). For example, first() does similarly odd things.
count(), like in RDBMS, computes the sum of all the records in only one value. For your purpose .size()seems the right method to call:
in('written_by').size()

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 Multiple IN statements on one column

Okay, I'm using WordPress, but this pertains to the SQL side.
I have a query in which I need to filter out posts using three different categories, but they're all terms in the post.
For example:
In my three categories, I select the following: (Academia,Webdevelopment) (Fulltime,Parttime) (Earlycareer).
Now what I want to do is make sure when I query that the post has AT LEAST ONE of each of those terms.
CORRECT RESULT: A post with tags Academia, Fulltime, Earlycareer
INCORRECT RESULT: A post with tags Academia, Earlycareer (doesn't have fulltime or parttime)
Currently, my query looks something like this:
SELECT * FROM $wpdb->posts WHERE
(
$wpdb->terms.slug IN (list of selected from category 1) AND
$wpdb->terms.slug IN (list of selected from category 2) AND
$wpdb->terms.slug IN (list of selected from category 3)
)
AND $wpdb->term_taxonomy.taxonomy = 'jobtype' AND .......
When using this query, it returns no results when I select across the different categories (that is, I can choose 4 things from category 1 and it has results, but I can't choose anything from category 2 or 3. And vice versa)
I'm not sure if this is something to do with using IN more than once on the same column.
Thanks in advance for any help!
Your query seems to be correct. There is no any limitations in SQL about using IN for the same column miltimple times.
But ensure that you don't have any NULL values in your list of selected from category 1/2/3 queries. Even single NULL value in these lists will give NULL as a result of whole 'WHERE' condition and you will get nothing as a result.
If this won't help then it must be WordPress issue.