一个SQL查询的问题???

zhuwei923198101 2007-10-31 11:45:55
有一个表,表中的记录如下:
序号 工号 数量 日期
6 2681 1100.0 2007-05-05
7 2681 1250.0 2007-10-15
9 2157 1400.0 2007-10-17
10 2890 1200.0 2007-10-17
11 1791 1700.0 2007-10-17
13 2746 1200.0 2007-10-18
14 2890 1350.0 2007-10-18
15 3220 1400.0 2007-10-18
16 3094 1400.0 2007-10-18
17 2087 1350.0 2007-10-24
18 3175 1350.0 2007-10-26
19 2890 1450.0 2007-10-26

现在我想用一条查询语句,将其中各工号中数量最大的‘记录’的序号找出来????

我用select 工号,数量 form 表 group by 序号 ,能找出各工号中数量最大的记录,但是得不到我想要的序号?苦想了很久,一直没有实现。。。。分不多,还请请大家多多帮助我 !
...全文
68 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pt1314917 2007-10-31
  • 打赏
  • 举报
回复

select a.* from 表 a where 数量 = (select max(数量) from 表 where 工号 = a.工号)
baihecheng 2007-10-31
  • 打赏
  • 举报
回复


select 工号,数量 ,序号 form 表 as a where 数量 in (select max(数量) from 表 group by 工号 having a.工号=工号)
dobear_0922 2007-10-31
  • 打赏
  • 举报
回复
--or
select * from 表 T
where not exists (select 1 from 表 where 工号=T.工号 and 数量>T.数量)
dawugui 2007-10-31
  • 打赏
  • 举报
回复
select a.* from tb a where 数量 = (select max(数量) from tb where 工号 = a.工号)
dobear_0922 2007-10-31
  • 打赏
  • 举报
回复
select 序号 from 表 T
where not exists (select 1 from 表 where 工号=T.工号 and 数量>T.数量)
dawugui 2007-10-31
  • 打赏
  • 举报
回复
--按某一字段分组取最大(小)值所在行的数据
--2007-10-23于杭州
/*
数据如下:
name val memo
a 2 a2(a的第二个值)
a 1 a1--a的第一个值
a 3 a3:a的第三个值
b 1 b1--b的第一个值
b 3 b3:b的第三个值
b 2 b2b2b2b2
b 4 b4b4
b 5 b5b5b5b5b5
*/
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
go

--一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name val memo
---------- ----------- --------------------
a 3 a3:a的第三个值
b 5 b5b5b5b5b5
*/

--二、按name分组取val最小的值所在行的数据。
--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值
*/

--三、按name分组取第一次出现的行所在的数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
*/

--四、按name分组随机取一条数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 5 b5b5b5b5b5
*/

--五、按name分组取最小的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
b 2 b2b2b2b2
*/

--六、按name分组取最大的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
a 3 a3:a的第三个值
b 4 b4b4
b 5 b5b5b5b5b5
*/
zhuwei923198101 2007-10-31
  • 打赏
  • 举报
回复
问题解决了,谢谢!!!

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧