开发者

how to create this query

how to create a query if i need to include two aggregate function in select row and per each function i need different group by and where conditions in my example i need to returns the playerName, and how many the player win the this can be checked if the results in table game result= first, and how many times he played

but do not know how to deal with two aggregate functions .

simply i want to join the result of this two queries

1.

select playeName,count(*)
from  player,game
where player.playerId=game.playerId and result="first"
group by game.playerId

2.

select count(*)
from game, player
where game.playerId=player.playerId
group by game.playerId

the set of attributes for table game are playerId , result the set of attributes 开发者_运维问答for table player are playerName,playerId

any idea???


Use:

  SELECT p.playername,
         SUM(CASE WHEN g.result = 'first' THEN 1 ELSE 0 END),
         COUNT(*)
    FROM PLAYER p
    JOIN GAME g ON g.playerid = p.playerid
GROUP BY p.playername


Along with solutions proposed by OMG Ponies and Bnjmn, you can also get desired results by using WITH ROLLUP

select result, count(*)
from game, player
where game.playerId=player.playerId
group by game.playerId, result WITH ROLLUP

Then, on client side, find records with result equals 'first' and and result is null(which is #games played).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜