Update join vs Update with sub query - sap-iq

I have 2 tables :
Table A
ID int,
Name varchar,
Address varchar
Table B
ID int,
Name varchar,
Address varchar,
Col1...
...
Col100
I want to update A.Name with B.Name based on both table's ID.
UPDATE A
SET A.NAME = B.NAME
FROM A JOIN (SELECT ID, Name from B) AS B ON A.ID = B.ID
UPDATE A
SET A.NAME = B.NAME
FROM A JOIN B AS B ON A.ID = B.ID
Because I don't have the permission to set the option for viewing execution plan, I ran 2 queries above many times and see the processed time to guess which one has better performance.
With about 700k records, both queries's result slightly differ.
So my question is, if executed, will both queries have the same execution plan? (I mean will the database engine process them by the same way? )
P/S : I'm using SybaseIQ 16.0.0.429
Update : I'm getting the permission for viewing the execution plan. In case both plan are identical, does is mean both queries will be processed the same (100% sure) ?

I got the execution plan.
Both queries have identical plan.
And Sybase automatically "reworded" the first query, the actual executed query is exactly the same with the second query :
UPDATE A
SET A.NAME = B.NAME
FROM A,B WHERE A.ID = B.ID
Conclusion : both queries are identical.

Related

SQL Join :: Fetching records outside join condition

I have 2 tables A and B
A
B
The requirement is to join both tables using id column and along with that, if the fetched name value is having another record with a different id, that record should also be fetched. Like the below screenshot.
Output :
Requirements
Table B is in the size of TBs. single join of both tables will be
preferable
query needs to be executed on hive
I'm not familiar with HiveQL, but with regular SQL, you'll need to join table B to itself a second time as part of the query.
select
b_name.id, b_name.name
from
#table_A a
join #table_B b -- This table gets the "name" value for lookup
on (a.id=b.id)
join #table_B b_name -- This is the table you want to pull your "output" from
on (b.name=b_name.name)
This query essentially says that you need to find the value of the "name" column in table B, where there is a matching ID in table A, and then lookup all the rows with that name value in table B.
You can join the same table multiple times. So in the query below, b1 will give you all the names for the ids in A, and b2 is joined by name, to get you all the extra ids that are not in A.
select
b2.*
from
A
inner join B b1 on b1.id = A.id
inner join B b2 on b2.name = b1.name

Returning an attribute of a node based on specific constraints from another node

I am trying to query the following:
List the Names of students who have at least one A in courses they are enrolled in. (Do not consider A- as an A.)
Image of the schema for my database
I am using the following query:
MATCH (studentPerson)<-[:S2P]-(:Student)-[:Taking]->(:Offering)-
[:Covers]->(studentCourse)
WHERE studentCourse.Grade = "A"
RETURN studentPerson.Name as student
But each time I run it, I keep getting "(no changes, no records)".
I also tried using EXISTS(studentCourse.Grade = "A") to no avail.
When the records were added to the database, they were added from a file like so:
match (a:Student), (b:Offering) where a.ID = 419180204 and b.ID = 15
create (a) - [r:Taking{EnrollmentID:'Enrollment306',Grade: 'A-'}]-> (b)
WITH count(*) as dummy
match (a:Student), (b:Offering) where a.ID = 449976666 and b.ID = 15
create (a) - [r:Taking{EnrollmentID:'Enrollment307',Grade: 'C+'}]-> (b)
WITH count(*) as dummy
match (a:Student), (b:Offering) where a.ID = 477453864 and b.ID = 15
create (a) - [r:Taking{EnrollmentID:'Enrollment308',Grade: 'A'}]-> (b)
WITH count(*) as dummy
match (a:Student), (b:Offering) where a.ID = 495490053 and b.ID = 15
create (a) - [r:Taking{EnrollmentID:'Enrollment309',Grade: 'A-'}]-> (b)
Is it possible that due to the entities being added into the relationship ("r:Taking") that the Offering node does not even have grades?
I'm brand new at neo4j and trying my best, but this stuff is so confusing to me so far.
Appreciate any help.
The problem is that Grade is a property of the :Taking relationship. So, in your query, add a variable to that relationship type, e.g. (:Student)-[t:Taking]->(:Offering) and use t.Grade = 'A' for filtering:
MATCH (studentPerson)<-[:S2P]-(:Student)-[t:Taking]->(:Offering)-
[:Covers]->(studentCourse)
WHERE t.Grade = `A`
RETURN studentPerson.Name as student

Solr outer join / not join query

I may be asking too much but I want to do a left outer join between two cores
and get data from A only where B does not have related data.
Following is exactly my equivalent SQL query (for simplicity I have removed other conditions),
1. SELECT A.* FROM A AS A
WHERE A.ID NOT IN (SELECT B.A_ID FROM B AS B WHERE B.STATUS_ID != 1)
I understand that solr join is actually subquery, I need data from only A.
It would be very easy if the not was not there in where condition for sub query.
For example,
2. SELECT A.* FROM A AS A
WHERE A.ID IN (SELECT B.A_ID FROM B AS B WHERE B.STATUS_ID != 1)
I can have q={!join from=aId to=id fromIndex=b}(-statusId:1).
How can I do a nagete here, i.e. solr query for 1

Neo4j Cypher MATCH if Not exists

In SQL where is an option (NOT EXISTS) that allows us to only select a row if there is zero results from other SELECT.
For example:
SELECT
c.CustomerKey
FROM
Customer c, Sales s1, Date d
WHERE
s1.CustomerKey = c.CustomerKey AND
s1.OrderDateKey = d.DateKey AND
s1.ShipDate > s1.DueDate AND
NOT EXISTS (SELECT *
FROM Sales s2
WHERE s2.OrderDateKey = s1.OrderDateKey AND
s2.CustomerKey <> s1.CustomerKey)
GROUP BY
c.CustomerKey
I tried to do the following but the query never ends so I assume I'm doing it the wrong way. What am I missing?
MATCH (d1:Date)<-[:ORDERDATE]-(s1:Sales)-[:CUSTOMER]->(c1:Customer)
WHERE s1.ShipDate > s1.DueDate
WITH d1,s1,c1
MATCH (s2:Sales)-[:CUSTOMER]->(c2:Customer)
WHERE NOT(s2.OrderDateKey=s1.OrderDateKey AND c2.CustomerKey<>c1.CustomerKey)
RETURN c2.CustomerKey
The query below should do what you want.
First, you should create an index on :Sales(OrderDateKey) so that the OPTIONAL MATCH in the query below can quickly find the desired Sales nodes (instead of scanning all of them):
CREATE INDEX ON :Sales(OrderDateKey);
When the OPTIONAL MATCH clause fails to find a match, it sets its unbound identifiers to NULL. The following query takes advantage of that fact:
MATCH (:Date)<-[:ORDERDATE]-(s1:Sales)-[:CUSTOMER]->(c1:Customer)
WHERE s1.ShipDate > s1.DueDate
WITH s1.OrderDateKey AS odk, c1.CustomerKey AS customerKey
OPTIONAL MATCH (s2:Sales)-[:CUSTOMER]->(c2:Customer)
WHERE s2.OrderDateKey=odk AND c2.CustomerKey<>customerKey
WITH customerKey
WHERE c2 IS NULL
RETURN DISTINCT customerKey;
The tricky part of translating SQL to Cypher is figuring out when we should still do joins and predicates based on keys, vs when we should be translating those operations into usages of nodes and relationships.
Let's first translate what the SQL means, as best as I can tell:
We want to match a Sale with a Customer and an order Date, where the
sale's ship date is past the due date, and there isn't already a Sale
with the same order Date for a different Customer.
It looks like Sale.OrderDateKey is a foreign key to Date.DateKey's primary key, and that Sales.CustomerKey is a foreign key to Customer.CustomerKey's primary key.
If the above assumption is true, then we don't need to work with these keys at all...where SQL uses foreign and primary keys for joining, Neo4j uses relationships between nodes instead, so we don't need to actually use these fields for anything in this query except the returned values.
MATCH (orderDate:Date)<-[:ORDERDATE]-(s1:Sales)-[:CUSTOMER]->(c1:Customer)
WHERE s1.ShipDate > s1.DueDate
WITH orderDate, c1
// match to exclude is a sale with the same orderDate but different customer
OPTIONAL MATCH (orderDate)<-[:ORDERDATE]-(:Sales)-[:CUSTOMER]->(c2:Customer)
WHERE c1 <> c2
WITH c1
WHERE c2 IS NULL
RETURN DISTINCT c1.customerKey;

oracle outer join query

I have 3 oracle tables. A joins to B and B joins to C. I want all records from A irerspective of whether a corresponding record exists in B or C. I wrote a query like this:
select a.name from a,b,c where a.a_id = b.b_id(+) and b.b_id = c.c_id(+)
This query does not seem right to me, particularly with the second join. What will exactly happen if there is a record in A but correspondingly nothing in B and C? Will it still fetch the record?
For some reason the above query returns same count of records as select a.name from a
So I am guessing that the query is right? Also is there a better way to rewrite the query?
I presume the better query can be
Select a.name from A a left join B b on a.a_id=b.b_id inner join C c on b.b_id=c.c_id
This should give the result as you have expected
http://rajanmaharjan.com.np

Resources