SQL聚合查询错误

发布于 2024-12-23 18:47:13 字数 625 浏览 0 评论 0原文

我有3个这样的表

player(id,name,age,teamid)
team(id,name,sponsor,totalplayer,totalchampion,boss,joindate)
playerdetail(id,playerid,position,number,allstar,joindate)

我想选择团队信息包括姓名,赞助商,总球员,总冠军,老板, 球员的平均年龄,全明星球员的数量

我编写了如下t-sql

SELECT T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE,
AVG(P.AGE) AS AverageAge,COUNT(D.ALLSTAR) As AllStarPlayer
FROM Team T,Player P,PlayerDetail D 
WHERE T.ID=P.TID AND P.ID=D.PID

,但它不起作用,错误消息是

'列'Team.Name'在选择列表中无效,因为它不是 包含在聚合函数或 GROUP BY 子句中。'

谁能帮助我? 提前谢谢!

I have 3 tables like this

player(id,name,age,teamid)
team(id,name,sponsor,totalplayer,totalchampion,boss,joindate)
playerdetail(id,playerid,position,number,allstar,joindate)

I want to select teaminfo include name,sponsor,totalplayer,totalchampion,boss,
the average age of the players, the number of the allstar players

I write the t-sql as below

SELECT T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE,
AVG(P.AGE) AS AverageAge,COUNT(D.ALLSTAR) As AllStarPlayer
FROM Team T,Player P,PlayerDetail D 
WHERE T.ID=P.TID AND P.ID=D.PID

but it doesn't work, the error message is

'Column 'Team.Name' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.'

Who can help me?
Thx in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦太阳 2024-12-30 18:47:13

GROUP BY 
      T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE

大多数 RDBMS 中(MySQL 除外,它会为您猜测),列必须是聚合的(COUNTAVG) 或在 GROUP BY 中

另外,您应该使用显式 JOIN。
更清晰、更清晰、更难以修改你的代码

SELECT 
    T.NAME, T.SPONSOR, T.TOTALPLAYER, T.TOTALCHAMPION, T.BOSS, T.JOINDATE,
    AVG(P.AGE) AS AverageAge,
    COUNT(D.ALLSTAR) As AllStarPlayer
FROM 
    Team T
    JOIN
    Player P ON T.ID=P.TID
    JOIN
    PlayerDetail D  ON P.ID=D.PID
GROUP BY 
    T.NAME, T.SPONSOR, T.TOTALPLAYER, T.TOTALCHAMPION, T.BOSS, T.JOINDATE;

Add

GROUP BY 
      T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE

In most RDBMS (except MySQL which will guess for you), a column must be either aggregated (COUNT, AVG) or in the GROUP BY

Also, you should use explicit JOINs.
This is clearer, less ambiguous and more difficult to bollix your code

SELECT 
    T.NAME, T.SPONSOR, T.TOTALPLAYER, T.TOTALCHAMPION, T.BOSS, T.JOINDATE,
    AVG(P.AGE) AS AverageAge,
    COUNT(D.ALLSTAR) As AllStarPlayer
FROM 
    Team T
    JOIN
    Player P ON T.ID=P.TID
    JOIN
    PlayerDetail D  ON P.ID=D.PID
GROUP BY 
    T.NAME, T.SPONSOR, T.TOTALPLAYER, T.TOTALCHAMPION, T.BOSS, T.JOINDATE;
来世叙缘 2024-12-30 18:47:13

鉴于您需要每个团队的这些数据,并且 team.ID 唯一标识团队,我建议如下:

SELECT max(T.NAME) As TeamName,
       max(T.SPONSOR) As Sponsor,
       max(T.TOTALPLAYER) As TotalPlayers,
       max(T.TOTALCHAMPION) As TotalChampions,
       max(T.BOSS) As Boss,
       max(T.JOINDATE) As JoinDate,
       AVG(P.AGE) AS AverageAge,
       COUNT(D.PID) As AllStarPlayer
FROM Team T
join Player P on T.ID=P.TID 
left join PlayerDetail D on P.ID=D.PID and D.ALLSTAR = 'Y'
group by T.ID

Given that you want this data per team, and team.ID uniquely identifies team, I suggest the following:

SELECT max(T.NAME) As TeamName,
       max(T.SPONSOR) As Sponsor,
       max(T.TOTALPLAYER) As TotalPlayers,
       max(T.TOTALCHAMPION) As TotalChampions,
       max(T.BOSS) As Boss,
       max(T.JOINDATE) As JoinDate,
       AVG(P.AGE) AS AverageAge,
       COUNT(D.PID) As AllStarPlayer
FROM Team T
join Player P on T.ID=P.TID 
left join PlayerDetail D on P.ID=D.PID and D.ALLSTAR = 'Y'
group by T.ID
牵你手 2024-12-30 18:47:13

使用:

SELECT T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE,
AVG(P.AGE) AS AverageAge,COUNT(D.ALLSTAR) As AllStarPlayer
FROM Team T
JOIN Player P ON T.ID = P.TEAMID
JOIN PlayerDetail D ON P.ID = D.PLAYERID
GROUP BY T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE

Use:

SELECT T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE,
AVG(P.AGE) AS AverageAge,COUNT(D.ALLSTAR) As AllStarPlayer
FROM Team T
JOIN Player P ON T.ID = P.TEAMID
JOIN PlayerDetail D ON P.ID = D.PLAYERID
GROUP BY T.NAME,T.SPONSOR,T.TOTALPLAYER,T.TOTALCHAMPION,T.BOSS,T.JOINDATE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文