简体   繁体   中英

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”).

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...

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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