I need to SUM two columns (pts_won) from two different tables (prognosis_scores and question_scores) but it doesn't work properly.
If there is many prognosis_scores or many question_scores the sum will be not accurate (For instance if there is one question_scores and three prognosis_scores the sum will add 3 times the pts_won of thee question_scores) :
Here the result of the sum will be 30 and i want it to be 10
| prognosis_scores | question_scores|
| ---------------- | -------------- |
| pts_won | pts_won |
Here is my code
CREATE VIEW score_calculations
AS SELECT
users.id as user_id,
users.name as name,
users.company_id as company_id,
users.team_id as team_id,
users.email_verified as email_verified,
-- users.email as email,
SUM(COALESCE(prognosis_scores."pts_won", 0) + COALESCE (question_scores."pts_won", 0) ) as pts_won,
SUM(prognosis_scores."good_gap") as good_gap,
SUM(prognosis_scores."good_score") as good_score,
SUM(prognosis_scores."isGoodPrognosis"::INT) as good_winner
FROM users
LEFT JOIN prognosis_scores
ON prognosis_scores.user_id=users.id
LEFT JOIN question_scores
ON question_scores.user_id=users.id
GROUP BY users.id , users.name, users.company_id,team_id,email_verified;
how can i solve this with just sum both one time ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…