Can multiple joins exist in a single sql statement - derby

A good example of this would be
SELECT * FROM A WHERE M LEFT OUTER JOIN N, P LEFT OUTER JOIN Q..

Related

Using Row Pivot Together

My Raw Query Is
select element1_name, element1_value, element2_name, element2_value
from reports r
I Want Convert element_name,element_value Rows To Columns So Write This Query
select *
from (select *
from (select element1_name,
element1_value,
element2_name,
element2_value
from reports r)
pivot(max(element1_value) as one
for element1_name in('C' as C, 'Si' as SI, 'P' as P)))
pivot(max(element2_value) as tow
for element2_name in('C' as C, 'Si' as SI, 'P' as P))
There Is A Way That Write Two Pivot Together Without Two Sub Query Like this
Select * (...) pivot element1,pivot element2
Question:How to optimize this query?
You can try below query -
select *
from (select element1_name,
element1_value,
element2_name,
element2_value
from reports) AS R
pivot(max(element1_value) for element1_name in('C' as C, 'Si' as SI, 'P' as P)) as PV1
pivot(max(element2_value) for element2_name in('C' as C, 'Si' as SI, 'P' as P)) as PV2;

Relational Algebra: Natural Join having the same result as Cartesian product

I am trying to understand what will be the result of performing a natural join
between two relations R and S, where they have no common attributes.
By following the below definition, I thought the answer might be an empty set:
Natural Join definition.
My line of thought was because the condition in the 'Select' symbol is not met, the projection of all of the attributes won't take place.
When I asked my lecturer about this, he said that the output will be the same as doing a cartezian product between R and S.
I can't seem to understand why, would appreciate any help )
Natural join combines a cross product and a selection into one
operation. It performs a selection forcing equality on those
attributes that appear in both relation schemes. Duplicates are
removed as in all relation operations.
There are two special cases:
• If the two relations have no attributes in common, then their
natural join is simply their cross product.
• If the two relations have more than one attribute in common,
then the natural join selects only the rows where all pairs of
matching attributes match.
Notation: r s
Let r and s be relation instances on schema R and S
respectively.
The result is a relation on schema R ∪ S which is
obtained by considering each pair of tuples tr from r and ts from s.
If tr and ts have the same value on each of the attributes in R ∩ S, a
tuple t is added to the result, where
– t has the same value as tr on r
– t has the same value as ts on s
Example:
R = (A, B, C, D)
S = (E, B, D)
Result schema = (A, B, C, D, E)
r s is defined as:
πr.A, r.B, r.C, r.D, s.E (σr.B = s.B r.D = s.D (r x s))
The definition of the natural join you linked is:
It can be broken as:
1.First take the cartezian product.
2.Then select only those row so that attributes of the same name have the same value
3.Now apply projection so that all attributes have distinct names.
If the two tables have no attributes with same name, we will jump to step 3 and therefore the result will indeed be cartezian product.

Shuffle Join in Hive optimisation and understanding

Warning: Shuffle Join JOIN[38][tables = [a, b]] in Stage 'Stage-2:MAPRED' is a cross product
What does shuffle join mean?
What does JOIN[38][tables = [a, b]] signify? Does index 38 mean something special? Can I use this statement to reach the part of my query which needs optimisation?
PS : I have multiple shuffle joins happening in my query.

linq join with case condition

Hi may i know how to do a select "case" condition in using linq?
The commented out code are my question. how do i put the condition there?
my code:
var r = from u in Users
join p in Payments on u.Id equals p.UserId
join soi in SaleOrderItems on p.ReferenceId equals soi.Id
//if soi.InventoryTypeId == 1
//then join i in Inventories on soi.InventoryOrCourseId equals i.Id
//elseif soi.InventorytypeId ==2
//then join c in Courses on soi.InventoryOrCourseId equals c.Id
where u.Id == 5
select new{ u, p, soi, either i or c};
You have to use some outer join trick to accomplish this, one straightforward method is via DefaultIfEmpty(). Essentially you create an inner join then expand it with missing rows:
var r = from u in Users
join p in Payments on u.Id equals p.UserId
join soi in SaleOrderItems on p.ReferenceId equals soi.Id
join i in Inventories on new {a = soi.InventoryTypeId, b = soi.InventoryOrCourseId } equals new {a = 1, b = i.Id} into g1
from oi in g1.DefaultIfEmpty()
join c in Courses on new {a = soi.InventoryTypeId, b = soi.InventoryOrCourseId } equals new {a = 2, b = c.Id} into g2
from oc in g2.DefaultIfEmpty()
where u.Id == 5
select new{ u, p, soi, ic = oi ?? oc};
Be careful about this last statement ic = oi ?? oc, since the types differ the anonymous type will use System.Object declaration so it can accommodate both types, if you want to use strong typed support maybe a better option would be to return both oc and ic and then test. You should best decide that based on how you use this query late ron.

Oracle Left outer join

SELECT
a,
last_note_user,
c,
d,
iso_src
FROM
X
CROSS JOIN Y
CROSS JOIN Z
LEFT OUTER JOIN W
ON W.last_note_user = Z.userid
AND W.user_ten = Y.iso_src
The above ANSI code fetch me 107 records,When I giving the same query without ANSI code it is fetching 875 records.The non ANSI query is below:
SELECT
a,
last_note_user,
c,
d,
iso_src
FROM
X,
Y,
Z,
W
WHERE
W.last_note_user = Z.userid(+)
AND W.user_ten = Y.iso_src(+)
why there is difference in the two query with ANSI and without ANSI standards??
By answering the above query please help me out!!!
Your old-style query has the (+) symbols on the wrong side of the predicate. It should be:
SELECT
a,
last_note_user,
c,
d,
iso_src
FROM
X,
Y,
Z,
W
WHERE
W.last_note_user (+) = Z.userid
AND W.user_ten (+) = Y.iso_src
But I wouldn't use the old-style syntax any more really.
OUTER JOINS with old ANSI-syntax are ambiguous, so who knows what the query optimizer understands with this.
If the first SQL is producing the right rows, forget about the ANSI version and move on.

Resources