Answer the question
In order to leave comments, you need to log in
How to write a complex SQL query correctly?
There are 3 tables
TABLE1 id, date, opt
TABLE1 id, date, opt
TABLE1 id, date, opt
TABLE2 id, num, qty, f1(->TABLE1.id), f2(->TABLE3.id) many entries for TABLE1
TABLE2 id, num, qty, f1(->TABLE1.id), f2(->TABLE3.id)
TABLE2 id, num, qty, f1(->TABLE1.id), f2(->TABLE3.id)
TABLE3 id, name
TABLE3 id, name
TABLE3 id, name
What you need: get the sum of the fields TABLE2.num*TABLE2.qty
Where TABLE1.date == current month AND TABLE.opt != NULL
Where TABLE2.f2 == TABLE3.id
group by TABLE3 .id
Answer the question
In order to leave comments, you need to log in
SELECT
t3.`id`, SUM(t2.`num` * t2.`qty`)
FROM `table1` t1
JOIN `table2` t2 ON t2.`f1` = t1.`id`
JOIN `table3` t3 ON t3.`id` = t2.`f2`
WHERE t1.`date` >= '2019-09-01' AND NOT t1.`opt` IS NULL
GROUP BY t3.`id`
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question