your first query is standard SQL (the parentheses are redundant, only needed by MS-Access) and should work just fine in SQLite, with or without the parentheses:
SELECT
Message.messageContent, Message.messageDateTime
FROM
Message
INNER JOIN
( Contact INNER JOIN contact_m2m_message
ON Contact.contactID = contact_m2m_message.contactID
)
ON Message.msgID = contact_m2m_message.messageID ;
Edit: Actually, SQLite needs the parentheses, too, at least the version provided in SQL-Fiddle.
The 2nd query is not valid because you have not provided an alias for the derived table:
SELECT
Message.messageContent, Message.messageDateTime
FROM
Message
INNER JOIN
( Select Contact.contactNumber, Contact.contactName
FROM Contact INNER JOIN contact_m2m_message
ON Contact.contactID = contact_m2m_message.contactID
) --<-------------------------------------- no alias
ON Message.msgID = contact_m2m_message.messageID;
To make it work wirh a derived table, you have to provide an alias for the derived table and also add the contact_m2m_message.messageID
in the SELECT
list of the derived table (and remove columns that are not used):
SELECT
Message.messageContent, Message.messageDateTime
FROM
Message
INNER JOIN
( Select -- Contact.contactNumber, Contact.contactName,
contact_m2m_message.messageID
FROM Contact INNER JOIN contact_m2m_message
ON Contact.contactID = contact_m2m_message.contactID
) AS cm
ON Message.msgID = cm.messageID;
But I don't think you should use that. The first query should work. You can re-arrange the order how of the tables are joined. Using table aliases also helps in readibility:
SELECT
m.messageContent, m.messageDateTime
FROM
Message AS m
INNER JOIN
contact_m2m_message AS cm
ON m.msgID = cm.messageID
INNER JOIN
Contact AS c
ON c.contactID = cm.contactID ;
You want to group by
again the result of the union. For that you need to enclose the union in parentheses and add an alias ("name" it), i.e. make it a derived table:
select datum, sum(resColA) as resColA, sum(resColB) as resColB
from
( select datum, sum(colA) as resColA, 0 as resColB
from TableA
group by datum
union
select datum, 0 as resColA, sum(colB) as resColB
from TableB
group by datum
) as drv -- an alias for the derived table
group by datum
order by datum ;
or make it a CTE (common table expression):
with
cte as
( select datum, sum(colA) as resColA, 0 as resColB
from TableA
group by datum
union
select datum, 0 as resColA, sum(colB) as resColB
from TableB
group by datum
)
select datum, sum(resColA) as resColA, sum(resColB) as resColB
from cte
group by datum
order by datum ;
There is practically little difference between the above 2 options and I think no difference in efficiency in DB2. Pick whatever feels more readable to you. Alternatively, you could first union
the data from the 2 tables and then group by
. This will probably result in different execution plans, so test which is more efficient:
select datum, sum(resColA) as resColA, sum(resColB) as resColB
from
( select datum, colA as resColA, 0 as resColB
from TableA
union
select datum, 0 as resColA, colB as resColB
from TableB
) as drv
group by datum
order by datum ;
Best Answer
I was running the query inside python 2.7.10, which comes with sqlite 3.8.3.1. It turns out that if I run this directly with the sqlite 3.8.10.2 that comes with OSX, it works fine.
I upgraded my python sqlite to 3.9.1 and it works now.
Instructions per trac.edgewall.org/wiki/PySqlite
Run this command from the folder:
python setup.py build_static install
Note: I'm using OSX El Capitain, and a separate python installation (not the built in apple one) downloaded from python.org, and my .bash_profile is set up to point to this python installation