I need to join two tables with 'between' condition in ClickHouse without equal expressions.
How to implement this logic?
I did:
select a.*, b.name
from a join b
on a.dt between b.start_dt and b.end_dt;
Then got error
Code: 403, e.displayText() = DB::Exception: Invalid expression for JOIN ON. Expected equals expression...
now JOIN in Clickhouse supports equi-join only https://www.w3resource.com/sql/joins/perform-an-equi-join.php
You can use cartesian product and where
select a.*, b.name
from a , b
where a.dt between b.start_dt and b.end_dt;
it's absolutely fine.
Related
I have a subquery that is a union of two queries that I am quering from. I need to achieve the same results using TypeOrm. My query is as below.
select port_id,v.port_name,g.name as country from (
select destination_port_id as port_id from vessels
union
select last_port_id as port_id from vessels) as ports
join vessels_ports v on port_id=v.id join global_countries g on g.id = v.country_id
order by port_id asc;
I’m trying to join three tables. Two of these, N1 and N2, share an ID (PK) but they differ in the number of rows, so I have written two coalesce statements to clean up the PK and a related field:
SELECT
(SELECT COALESCE(N1.ID_Year, N2.ID_Year)) AS ID_Year,
(SELECT COALESCE(N1.ReceiverUniqueID, N2.ReceiverUniqueID)) AS AllNetworkRUID,
[…]
FROM N1
FULL OUTER JOIN N2
ON N1.ID_Year_RU = N2.ID_Year_RU
What I need to do is now join the third table, N3, to the second COALESCE field, ie to ‘AllNetworkRUID’, something like:
JOIN N3
ON N3.ID = AllnetworkRUID .
I’ve not been able to work out how to make that happen, of if it’s even possible.
Any suggestions?
When joining multiple tables, involving all join types (inner, left,cross, full etc), is there any specific order in which joins are evaluated, like we have BODMAS in Mathematics or it always reads from left to right in the order we specified.
I don't have the exact SQL but here is a sample SQL:
Select * from
A,
B
LEFT JOIN
C
ON B.ID = C.ID,
D
FULL JOIN
E
ON
D.ID = E.ID;
Will it be like A cross joining with B and the result Left joining with C and so on..?
OR
A cross joining with the Left join of B and C..
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
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