Chinaunix

标题: 如何写这个SQL语句啊 [打印本页]

作者: d02540220    时间: 2007-08-24 10:02
标题: 如何写这个SQL语句啊
想不出来该如何写了:
表结构
员工    部门     工资
1              1                  1000
2              1                  1500
3              1                  1300
4              2                  1800
5              2                  1700
6              2                  1500
7              2                  1600
8              3                  1400
9              3                  1200
现在要找出每个部门工资最高的员工的信息(员工,部门,工资)
谢谢了
作者: hxd001_810    时间: 2007-08-24 10:27
select a.员工,a.部门,b.最高工资
from a a inner join (select 部门,max(工资) 最高工资 from a group by 部门) b
on a.部门 = b.部门 and a.工资 = b.最高工资
order by a.员工
go
返回:
员工          部门          最高工资        
----------- ----------- -----------
2           1           1500
4           2           1800
8           3           1400

(所影响的行数为 3 行)
作者: chenlunpan    时间: 2007-08-24 10:35
select hh.id,hh.depart,hh.record from test as hh where

hh.record = (select max(record) from test where depart=hh.depart)
作者: chenlunpan    时间: 2007-08-24 10:51
select hh.id,hh.depart,hh.record from test as hh where

hh.record = (select max(record) from test where depart=hh.depart)order by id
作者: licup123    时间: 2007-08-24 17:56
标题: 我想知道楼上的说的一个问题
为什么在没用order by id这个语句的结果会跟用了之后的结果在顺序上有不同
没用order by id
id    depart   record
8       3          1400
4       2          1800
2       1          1500
用了order by id
id    depart   record
2       1          1500
4       2          1800
8       3          1400
作者: syspingz    时间: 2007-08-24 20:08
提示: 作者被禁止或删除 内容自动屏蔽
作者: kajaje    时间: 2007-08-24 21:56
slect * from table1 where 工资 in (select max(工资) from table1 group by 部门)
作者: licup123    时间: 2007-08-24 22:32
标题: 您的语句有逻辑错误
7楼的同志跟我以前犯过一样的错误
作者: kajaje    时间: 2007-08-24 22:57
标题: Re: 您的语句有逻辑错误
最初由 licup123 发布
[B]7楼的同志跟我以前犯过一样的错误 [/B]


是错了




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2