MsSQL每年展示最赚钱的员工

时间:2014-07-01 15:20:29

标签: sql sql-server

很久以前我研究过sql,我刚刚得到这份工作,所以我猜这里生病了... 无论如何,我试图找到员工,他的销售利润是每年最高的 我知道它不是很好,但这就是我得到的:

create view V_Company_Share as
select sum(s.actualprice-s.ownershare)as 'companyShare',year(s.saledate) as 'year'
from sale s, SalesMan sm
where s.SalesmanID=sm.SalesManID
group by year(s.saledate)

select distinct v.year,sm.FirstName+' '+sm.LastName as 'name', v.companyShare
from V_Company_Share v, SalesMan sm,sale s
where v.year=year(s.SaleDate) and s.SalesmanID=sm.SalesManID

感谢分配给帮助者!

2 个答案:

答案 0 :(得分:0)

尝试此查询.....

    select Max(companyShare),year,SalesmanID (
    select sum(s.actualprice-s.ownershare)as [companyShare]
    ,year(s.saledate) as [year]
    ,s.SalesmanID as [SalesmanID]
    from sale s, SalesMan sm
    where s.SalesmanID=sm.SalesManID
    group by year(s.saledate)
    )
    group by year

答案 1 :(得分:0)

尝试此查询...

select Max(companyShare),year,SalesmanID from
(
    select sum(s.actualprice-s.ownershare)as [companyShare]
    ,year(s.saledate) as [year]
    ,s.SalesmanID as [SalesmanID]
    from sale s, SalesMan sm
    where s.SalesmanID=sm.SalesManID
    group by year(s.saledate)
)A
group by year

如果逻辑错误,请尝试使用o / p

发布演示数据