![](/img/trans.png)
[英]Slow SQL query using “SELECT TOP” generated by Linq Take()
[英]Select top 3 by Take() in Linq
我有這張桌子
EMPs(EmpNo int primary key identity(001,1),
Ename nvarchar(50),
Job nvarchar(20),
Mgr int,
HireDate datetime,
Sal int,
Comm int,
DeptNo)
我想選擇前三名是HiSal?
(from a in EMPs
group a by true into r
select r.OrderByDescending(x => x.Sall +(x.Comm != null ? x.Comm :0))).Take(3)
看來您是按最高薪水(薪金+佣金)排序,然后嘗試選擇前三名。
我認為您正在使其變得比必須的更加復雜。 嘗試這個:
var top3salaries = EMPs.OrderByDescending(x => x.Sal + x.Comm).Take(3);
您的查詢還顯示佣金可以為null
。 如果是這樣的話:
var top3salaries = EMPs.OrderByDescending(x => x.Sal + (x.Comm ?? 0)).Take(3);
試試這個查詢:
select top(3) EName, MAX(salary) as Hisal
from EMPLOYEES
group by EMPLOYEE_ID
order by MAX(salary) desc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.