如何使用SQL乘以值

时间:2010-11-10 08:12:46

标签: sql oracle

好的,所以我正在完成我的作业,并且无法弄清楚如何与SQL相乘以及如何正确地进行订购。

我应该“创建一个列出玩家(player_name),当前工资和新(创建)列的查询,该列反映工资增加10%(以工资* 1.1计算)。按降序顺序排列数据工资“。

这只是一个简单的NHL数据库,我们的老师弥补了这项任务。球员表中有74名球员。选手表中的字段为:player_id,player_name,player_salary,team_id和position_id。

这是我到目前为止所做的:

SELECT player_name, player_salary, SUM(player_salary*1.1) AS NewSalary
FROM players
GROUP BY player_salary, player_name;

这种方式至少可以运行。我相信我必须将GROUP BY更改为ORDER BY并在最后使用DESC标记,但这不起作用。这是我从运行此查询得到的输出:

PLAYER_NAME          PLAYER_SALARY          NEWSALARY              
-------------------- ---------------------- ---------------------- 
Johan Franzen        42000                  46200                  
Brad Stuart          18000                  19800                  
Tomas Holmstrom      38000                  41800                  
Dan Cleary           10000                  11000                  
Jonathan Toews       32000                  35200                  
David Krejci         28000                  30800                  
Mike Ribeiro         10000                  11000                  
Steve Sullivan       20005                  22005.5                
Ryan Getzlaf         28000                  30800                  
Chris Stewart        18000                  19800                  
Brad Richards        10000                  11000                  
Nathan Horton        20000                  22000                  
James Neal           38000                  41800                  
Nicklas Lidstrom     44000                  48400                  
Jiri Hudler          28000                  30800                  
TJ Oshie             44000                  48400                  
Blake Comeau         10000                  11000                  
Drew Stafford        26888                  29576.8                
Brenden Morrow       10000                  11000                  
Daniel Sedin         26000                  28600                  
PA Parenteau         43000                  47300                  
Henrik Zetterberg    32000                  35200                  
Valtteri Filppula    28000                  30800                  
Tomas Kopecky        26000                  28600                  
Andrei Kostitsyn     28000                  30800                  
Marian Hossa         44000                  48400                  
Henrik Sedin         10000                  11000                  
Don Smith            20520                  22572                  
Rick Nash            15750                  17325                  
Todd Bertuzzi        43000                  47300                  
Patrick Eaves        10000                  11000                  
Mike Modano          80000                  88000                  
Alex Goligoski       28000                  30800                  
Patrick Kane         44000                  48400                  
Bobby Ryan           26000                  28600                  
Dustin Brown         10000                  11000                  
Patrick Sharp        43000                  47300                  
John-Michael Liles   10000                  11000                  
Paul Stastny         14000                  15400                  
Matt Cullen          10000                  11000                  
Martin St Louis      32000                  35200                  
Alexander Semin      28000                  30800                  
Niklas Kronwall      10000                  11000                  
John Tavares         42000                  46200                  
Matt Moulson         38000                  41800                  
Tobias Enstrom       42000                  46200                  
Matt Duchene         18000                  19800                  
Steven Stamkos       32000                  35200                  
Sidney Crosby        42000                  46200                  
Teemu Selanne        10000                  11000                  
Daniel Alfredsson    10000                  11000                  
Evgeni Malkin        10000                  11000                  
Andrew Ladd          20000                  22000                  
Corey Perry          28000                  30800                  
Adam Keefe           315000                 346500                 
Brian Rafalski       20000                  22000                  
Darren Helm          10000                  11000                  
Brandon Dubinsky     28000                  30800                  
Mark Letestu         10000                  11000                  
Loui Eriksson        20000                  22000                  
Clarke MacArthur     42000                  46200                  
Kris Letang          30000                  33000                  
Pavel Datsyuk        26000                  28600                  
James Wisniewski     32000                  35200                  
Nicklas Lidstrom     43000                  47300                  
Milan Hejduk         18000                  19800                  
Tyler Ennis          43000                  47300                  
Paul Martin          38000                  41800                  
Derek Roy            28000                  30800                  
Mikko Koivu          10000                  11000                  
Joe Pavelski         20000                  22000                  
Joe Thornton         10000                  11000                  
Phil Kessel          26000                  28600                  
Alex Ovechkin        18000                  19800                  

74 rows selected

我可能会忽略一些相对简单的事情,但我没有在教科书中找到类似的参考点。非常感谢任何帮助。

4 个答案:

答案 0 :(得分:13)

为什么要使用GROUP BY?

SELECT player_name, player_salary, player_salary*1.1 AS NewSalary
FROM players
ORDER BY player_salary DESC

答案 1 :(得分:3)

你为什么要分组?你的意思是订单吗?

SELECT player_name, player_salary, player_salary * 1.1 AS NewSalary
FROM players
ORDER BY player_salary, player_name;

答案 2 :(得分:1)

您不需要使用GROUP BY,但使用它不会改变结果。只需在末尾添加ORDER BY行即可对结果进行排序。

SELECT player_name, player_salary, SUM(player_salary*1.1) AS NewSalary
FROM players
GROUP BY player_salary, player_name;
ORDER BY SUM(player_salary*1.1) DESC

答案 3 :(得分:0)

这是:

select player_name, player_salary, (player_salary * 1.1) as player_newsalary
from player 
order by player_name, player_salary, player_newsalary desc

如果表格中只有一个玩家实例,则无需“分组依据”。