这一条SQL怎么写?

ItOldHorse 2003-12-10 11:28:44

有如下这个表tabel1

dwbm sbkj sbhh

A0126 12 1011
A0126 50 1012
A0126 70 1010


如何生成这样的一个表table2

dwbm sbkj sbhh

A0126 70 1013

要求按dwbm进行汇总生成表 sbkj 要求取最大值,sbhh要求取sbkj取最大值时的sbhh


我是这样一来写的
select max(dwbm) as dwbm,max(sbkj) as sbkj ... from table1
...此处我不知道怎么写啦?
...全文
70 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dlpseeyou 2003-12-10
  • 打赏
  • 举报
回复
如果sbhh 是int整的
select dwbm dwbm ,max(sbkj) sbkj,sbhh=(select max(sbhh)+1 from table1 ) from table1 group by dwbm
Doncy 2003-12-10
  • 打赏
  • 举报
回复
select * from table1 where sbkj = ( select max(sbkj) from table1)
zjcxc 元老 2003-12-10
  • 打赏
  • 举报
回复
如果表已经存在,就用:

insert into table2
select * from table1 a
where sbkj=(select max(sbkj) from table1 where dwbm=a.dwbm)
zjcxc 元老 2003-12-10
  • 打赏
  • 举报
回复
select * into table2
from table1 a
where sbkj=(select max(sbkj) from table1 where dwbm=a.dwbm)
yangq4000 2003-12-10
  • 打赏
  • 举报
回复
select dwbm, max(sbkj) as [sbkj],case when sbkj=max(sbkj) then sbhh else null end)
from tablename group by dwbm
jacklinchen 2003-12-10
  • 打赏
  • 举报
回复
insert into table2(dwbm,sbkj,sbhh) (select dwbm,max(sbkj),max(sbhh)+1 from table1 group by dwbm)

34,590

社区成员

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

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