
Untitled
By: a guest on
Aug 10th, 2012 | syntax:
None | size: 1.01 KB | hits: 8 | expires: Never
how to print a sum of column in all rows in sql
'player name' 'number of goals'
'john' '10'
'martin' '20'
'jen' '20'
'player name' 'number of goals' 'Total goals by all players'
'john' '10' '50'
'martin' '20' '50'
'jen' '20' '50'
'player name' 'number of goals' '% goals scored' 'Total goals by all players'
'john' '10' '20' '50'
'martin' '20' '40' '50'
'jen' '20' '40' '50'
SELECT
a.player_name,
a.number_of_goals,
(a.number_of_goals / b.goalsum)*100 AS percent_scored,
b.goalsum
FROM
myPlayerInfo a
CROSS JOIN
(SELECT SUM(number_of_goals) AS goalsum FROM myPlayerInfo) b
select
player_name,
number_of_goals,
(number_of_goals * 100) / SUM(number_of_goals) OVER () AS percent_goals_scored,
SUM([number of goals]) OVER () AS total_goals_by_all_players
from myPlayerInfo