I've got a really big problem, and it stems from a table with 50k+ records.
This table looks something like this (+15 or so more columns that aren't too important):
table_1
date | name | email | num_x | num_y
I also have another table ON A DIFFERENT DB (same server) that looks something like this (+1 not important column):
table_2
name | comment | status
table_1 is updated daily with new entries (it is a feed table for use on other projects), which means there are a lot of repeat "name" rows. This is intended. table_2 contains comments and status notes about "name"s, but no repeat "name"s.
I need to write a query that will select all "name"s from table_1 where the total of all num_x + num_y > X. So, for example, if this were a few rows...
2010-11-19 | john.smith | [email protected] | 20 | 20
2010-11-19 | joel.schmo | [email protected] | 10 | 10
2010-11-18 | john.smith | [email protected] | 20 | 20
2010-11-18 | joel.schmo | [email protected] | 10 | 10
.. and I needed to find all "name"s with total num_x + num_y > 50, then I'd return john.smith | [email protected] | 80 . I would also return john.smith's status and comment from the other DB.
I wrote a query that I believe works fine, but it's problematic because it takes forever and a day to run. I also successfully retrieve records from the other db (I don't have that listed below).
SELECT
name,
email,
SUM(num_x + num_y) AS total
FROM
table_1
GROUP BY
name
HAVING
SUM(num_x + num_y) > 100
ORDER BY
total ASC
Is there a better way to go about this?
Thanks everyone!
Dylan
num_x + num_y?