简体   繁体   English

SUM SQL 多表 GROUP BY

[英]SUM SQL multiple tables GROUP BY

Hello I can't seem to get this question answered.您好,我似乎无法回答这个问题。

在此处输入图片说明

the question is : 3.5 For each horse that has won a prize, list the horse name, horse_id and total prize money won.问题是: 3.5 对于每匹赢得奖品的马,列出马名、horse_id 和赢得的总奖金。

have tried:试过:

SELECT horse_name, horse.horse_id, SUM(prizemoney) 
FROM horse 
JOIN entry ON entry.horse_id=horse.horse_id 
JOIN prize ON entry.event_id=prize.event_code 
GROUP BY horse_name

dont get the right answer没有得到正确答案

This is my answer:这是我的回答:

在此处输入图片说明

This is the teachers answer:这是老师的回答:

在此处输入图片说明

If you ignore PLACE in your joins then you will get the wrong answer.如果您在连接中忽略 PLACE,那么您将得到错误的答案。 Include PLACE in the join logic!在连接逻辑中包含 PLACE!

SELECT horse_name, horse.horse_id, SUM(prizemoney) 
FROM horse 
JOIN entry ON entry.horse_id=horse.horse_id 
JOIN prize ON entry.event_id=prize.event_code 
          AND entry.place = prize.place
GROUP BY horse_name, horse.horse_id

also: include ALL non-aggregating columns in the grouop by clause还:在 group by 子句中包含所有非聚合列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM