Click to See Complete Forum and Search --> : Why doesn't SUM work????
NotionCommotion
04-12-2006, 03:34 AM
Anyone know why the following doesn't work? I believe I may have to "group" them, but am not sure on the process. Thank you!
SELECT SUM(t1.c1) AS t1c1_sum, t2.c1 AS t2c1 FROM t1, t2;
chazzy
04-12-2006, 01:13 PM
how are these 2 tables related? you can't just group things of data that aren't related.
NotionCommotion
04-12-2006, 02:11 PM
I should have added that each table has another column called user_id. t1 might have multiple rows with a given user_id, but t2 will have only 1 row per user_id. The query should only return results when the user_id is equal to 1 (or whatever).
Thanks
chazzy
04-12-2006, 03:42 PM
since you're still being vague, the only thing i can say is that the group by clause looks like this:
GROUP BY column_name;
where column name is typically whatever you have in your sum.
NotionCommotion
04-13-2006, 01:22 AM
Wasn't trying to be elusive. The "whatever" was meant to imply that the id might change. This seems to work. The GROUP was actually on the non-summed column and I expect this was to ensure that only one row is returned. This is still very foriegn to me, and I am sure you will see more beginner questions from me. Thank you for your help.
SELECT SUM(t1.c1) AS t1c1_sum, t2.c1 AS t2c1
FROM t1
JOIN t2 ON t1.user_id=t2.user_id
WHERE t2.user_id=1
GROUP BY t2.c1;