2017-02-06 104 views
0

Employee行的T-SQL返回數包含四列:,最大價值和最高價值本身新創建的列

employee_id, name, salary, months 

enter image description here

任務:與salary * months最大值行返回數而這個最大值,我的最後一次嘗試是:

SELECT
months*salary FROM employee WHERE salary*months = (SELECT MAX(months*salary) FROM Employee)
加入COUNT(months*salary)不起作用

回答

0
SELECT * FROM Employee 
WHERE salary * months = (SELECT max(salary * months) FROM Employee) 

如果你只是想知道有多少條記錄,則:

Select employee_id, name, salary, months, 
     Max(months * salary) Over (Order By (Select Null)) As MaxSalary 
From Employee 

這將返回所有行並添加第5列:

SELECT count(1), max(salary * month) FROM Employee 
WHERE salary * months = (SELECT max(salary * months) FROM Employee) 
0

您可以用窗口MAX()做到這一點MaxSalary與每個行具有相同值的結果 - 記錄集中的最大值months * salary