freddyD
Programmer
- May 3, 2009
- 3
HI,
New to the forums and I apologize if this isn't being posted in the right area...
I am trying to run a pretty complex query (complex for me!!) and I have gotten the basic results i need but I am stuck on getting over the last hurdle.
I presently have this query that does what i need it to do.
SELECT SUM(activity)
FROM activities
WHERE activity_id IN(SELECT name_id FROM foods GROUP BY name_id HAVING SUM(points) > 20);
I need to add another part to this query but it has gotten a little complicated for me. There are three tables....dates, foods, activities and i need to find the sum of a result set which contains the minimum number between two values from two different tables as long as a certain statement is true.
Basically..
SELECT SUM(total)
FROM (SELECT MIN(value from table1 which is determined by a value in table2, value from table3) AS total
FROM table3
WHERE value from table3 is contained in a result set from table1);
The below query is something I came up with that is actually what i need but it doesn't work. I am doing something wrong...
SELECT SUM(activity_amount)
FROM (SELECT min((SELECT SUM(points) - 20 FROM foods WHERE name_id IN(SELECT pk FROM dates WHERE weekly=1) GROUP BY name_id), activity) AS activity_amount
FROM activities
WHERE activity_id IN(SELECT name_id FROM foods GROUP BY name_id HAVING SUM(points) > 20));
I believe the problem is with the first value in the MIN()...."SELECT SUM(points) - 20 FROM food WHERE name_id IN(SELECT pk FROM dates WHERE weekly=1) GROUP BY name_id"
That statement yields more than one value but even tho i do need those values to compare against others in the MIN(), I only need them one at a time...not as a whole set
Thanks for any help you might be able to provide
New to the forums and I apologize if this isn't being posted in the right area...
I am trying to run a pretty complex query (complex for me!!) and I have gotten the basic results i need but I am stuck on getting over the last hurdle.
I presently have this query that does what i need it to do.
SELECT SUM(activity)
FROM activities
WHERE activity_id IN(SELECT name_id FROM foods GROUP BY name_id HAVING SUM(points) > 20);
I need to add another part to this query but it has gotten a little complicated for me. There are three tables....dates, foods, activities and i need to find the sum of a result set which contains the minimum number between two values from two different tables as long as a certain statement is true.
Basically..
SELECT SUM(total)
FROM (SELECT MIN(value from table1 which is determined by a value in table2, value from table3) AS total
FROM table3
WHERE value from table3 is contained in a result set from table1);
The below query is something I came up with that is actually what i need but it doesn't work. I am doing something wrong...
SELECT SUM(activity_amount)
FROM (SELECT min((SELECT SUM(points) - 20 FROM foods WHERE name_id IN(SELECT pk FROM dates WHERE weekly=1) GROUP BY name_id), activity) AS activity_amount
FROM activities
WHERE activity_id IN(SELECT name_id FROM foods GROUP BY name_id HAVING SUM(points) > 20));
I believe the problem is with the first value in the MIN()...."SELECT SUM(points) - 20 FROM food WHERE name_id IN(SELECT pk FROM dates WHERE weekly=1) GROUP BY name_id"
That statement yields more than one value but even tho i do need those values to compare against others in the MIN(), I only need them one at a time...not as a whole set
Thanks for any help you might be able to provide