Answer the question
In order to leave comments, you need to log in
Query with the calculation of the sum of the union table with between?
Good afternoon.
It is necessary to get all auditors who are associated with a certain age group, provided that in their connection the total number in each group is greater (value column of table 3);
The problem is because of table 4, because when combined it gives duplicates to table 2, and from here, with further combining and summation, the sum is obtained, taking into account duplicates, nor grouping on DISTINCT does not remove duplicates. And if you display the result without summation, but with grouping or DISTINCT, then the duplicates disappear, but not when summing. How to be?
1. auditors (auditors)
Its structure is not important here...
2. age groups (audiences)
3. age group and auditor relationship map (auditors_audiences_map)
4. age group disclosures (audiences_range_map)
Examples of requests, with and without grouping..
(
SELECT am2.id, SUM(am2.value)
FROM auditors_audiences_map AS am2
LEFT JOIN audiences_range_map AS a2 ON (a2.value BETWEEN 13 AND 17)
WHERE am2.audience_id=a2.audience_id
GROUP BY am2.audience_id
)
(
SELECT am2.id, am2.value
FROM auditors_audiences_map AS am2
LEFT JOIN audiences_range_map AS a2 ON (a2.value BETWEEN 13 AND 17)
WHERE am2.audience_id=a2.audience_id
GROUP BY am2.audience_id
)
Answer the question
In order to leave comments, you need to log in
I found a solution, I don't know how correct it is, but it works.
If someone has a more correct and optimized version, please tell me, and my solution looks like this:
SELECT SUM(am3.value) FROM
(
SELECT am2.id, am2.value
FROM auditors_audiences_map AS am2
LEFT JOIN audiences_range_map AS a2 ON (a2.value BETWEEN 13 AND 17)
WHERE am2.audience_id=a2.audience_id
GROUP BY am2.audience_id
) AS am3
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question