I have an rttmm table with conversation_id and duration fields. There's a query using two sub-queries in a FROM-clause, one of them is not used. I would expect it to be semantically equivalent to the one where you would remove the unused subquery, but it behaves very differently. Here's the query in question:
select
sum(subq2.dur) as res
from (
select sum(rttmm.duration) as dur, rttmm.conversation_id as conv_id
from rttmm
group by rttmm.conversation_id
) as subq1,
(
select sum(rttmm.duration) as dur, rttmm.conversation_id as conv_id
from rttmm
group by rttmm.conversation_id
) as subq2
and here's what I would expect it to be equivalent to (just removing the subq1):
select
sum(subq2.dur) as res
from
(
select sum(rttmm.duration) as dur, rttmm.conversation_id as conv_id
from rttmm
group by rttmm.conversation_id
) as subq2
Turns out it's not the same at all. What is the proper understanding of the first query here?
JOINsyntax. Easier to write (without errors), easier to read (and maintain), and easier to convert to outer join if needed.SELECTfrom a source inFROMorJOIN, all underlying tables are being used some way!JOINin the final code, basically needed to do two queries separately and combine their results. Ended up join doing two queries and combining the results not via SQL.