試試這個:
CREATE TABLE cust(CutomerID int,Salary float)
INSERT INTO cust
VALUES(1,2000),(2,3000),(3,4000),(4,6000),(5,5220)
CREATE TABLE dept(DeptID int,DeptNam varchar(10))
INSERT INTO dept
VALUES(1,'IT'),(2,'Finance'),(3,'Marketing')
CREATE TABLE custDept(CustomerID int,DeptID int)
INSERT INTO custDept
VALUES(1,1),(2,1),(3,2),(4,3),(5,3)
select a.CutomerID,a.DeptNam,b.sal from
(select c.CutomerID ,d.DeptID,c.Salary,d.DeptNam from cust c inner join custDept cd on
c.CutomerID = cd.CustomerID
inner join dept d
on cd.DeptID =d.DeptID) a
inner join
(select d.DeptID ,MAX(c.Salary) as sal from cust c inner join custDept cd on
c.CutomerID = cd.CustomerID
inner join dept d
on cd.DeptID =d.DeptID
group by d.DeptID) b
on a.DeptID = b.DeptID and a.Salary = b.sal
氣味功課。我錯了嗎? – Steve 2012-07-27 20:49:33
對不起只有SQL ...我只是盡我最大的努力學習像這樣得到不同asnwers .. – user957178 2012-07-27 20:51:29
請提供一個[sqlfiddle](http://sqlfiddle.com)的例子, – 2012-07-27 20:52:43