[英]how to merge count values in sql
I am trying to update all city values of the table team as: “city” + “ #p” + “number of players” +” g” + “number of goals forward” (eg “Tokyo #p25 g74”). 我正在尝试将桌队的所有城市值更新为:“城市” +“#p” +“球员人数” +“ g” +“前进目标数”(例如“东京#p25 g74”)。
I tried to do this and have this two queries. 我试图做到这一点,并有这两个查询。 One for get the number of players and one for get the number of goals. 一个用于获取玩家数量,一个用于获取目标数量。
Query for the number of players: 查询玩家人数:
select t.city + '#p' + CONVERT(varchar(10), count(pt.playerId)) + '#g'
from team t,
player_team pt
where pt.teamID = t.teamID
group By t.teamId,t.city
Query for the number of goals: 查询目标数量:
select count(*) totalgoals,
pt.teamID
from goals g,
player_team pt
where g.playerId = pt.playerId
group by pt.teamID
i couldn't merge theese two counts. 我无法将这两个因素合并。 Help me out pls... 请帮我...
Also my tables hierarchy and fields like the shown below 还有我的表层次结构和字段,如下所示
player
(
playerID int,
firstName nvarchar(25),
lastName nvarchar(25),
nationality varchar(25),
birthDate smalldatetime,
age smallint,
position varchar(25)
)
team
(
teamID int,
name nvarchar(50),
city nvarchar(25)
)
player_team
(
playerID int,
teamID int,
season varchar(5)
)
match
(
matchID int,
homeTeamID int,
visitingTeamID int,
dateOfMatch smalldatetime,
week tinyint
)
goals
(
matchID int,
playerID int,
isOwnGoal bit,
minute tinyint
)
EDIT : Select query with the given below is worked well and give me the right results.But how can i update the table with this multiple records? 编辑:使用下面给出的选择查询效果很好,并给了我正确的结果。但是我怎么用这个多条记录更新表? When i try to update it as a subquery into update statement, it gives me compile error and complaining about multirecords... 当我尝试将其作为子查询更新到update语句时,它给了我编译错误并抱怨多记录...
Error:Subquery returned more than 1 value. 错误:子查询返回了多个值。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。
Assuming your 2 queries are producing the desired results, try combining them with an an outer join
and a subquery: 假设您的2个查询产生了所需的结果,请尝试将它们与outer join
和子查询组合:
select pt.teamId,
t.city + ' #p' + CONVERT(varchar(10), count(pt.playerId))
+ ' g' + CONVERT(varchar(10), t2.totalgoals)
from team t
inner join player_team pt on pt.teamID = t.teamID
left join (
select count(*) totalgoals,
pt.teamID
from goals g inner join player_team pt on g.playerId = pt.playerId
group by pt.teamID
) t2 on t.teamid = t2.teamid
group By pt.teamId,t.city,t2.totalgoals
The simplest way probably is to use correlated subqueries to look up the values for each team: 最简单的方法可能是使用相关子查询来查找每个团队的值:
SELECT t.city || ' #p' ||
CONVERT(varchar(10), (SELECT count(*)
FROM player_team
WHERE teamId = t.teamId))
|| ' #g ' ||
CONVERT(varchar(10), (SELECT count(*)
FROM goals
JOIN player_team USING (playerId)
WHERE teamId = t.teamId))
FROM team t
Try this: 尝试这个:
SELECT a.city '#p' + CONVERT(varchar(10), a.playerCount) + '#g' + CONVERT(varchar(10), b.totalgoals)
FROM (SELECT t.teamId, t.city, count(pt.playerId) as playerCount
FROM team t JOIN player_team pt on (t.teamID = pt.teamID)
GROUP BY t.teamId, t.city)a JOIN
(SELECT pt.teamId, count(*) as totalgoals,
FROM goals g JOIN player_team pt on (g.playerId = pt.playerId)
GROUP BY pt.teamId) b ON (a.teamId = b.teamId)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.