求SQL 语句!

lynx800602 2003-08-22 10:51:19

表有三列:AA(主健),BB(NVARCHAR),CC(DATETIME)
要求查询出的结果按BB值相同的放在一起(ORDER BY),同时按CC倒序取前五行
如:BB字段有6个值是“TEST”那么只按CC取前5行
...全文
31 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
老宛 2003-08-22
  • 打赏
  • 举报
回复
你的问题要不要限定只能对一个表进行操作
老宛 2003-08-22
  • 打赏
  • 举报
回复
楼上的是错误的
z5wjz 2003-08-22
  • 打赏
  • 举报
回复
select top 5 * from table
order by cc desc,bb
lynx800602 2003-08-22
  • 打赏
  • 举报
回复
蚂蚁真是活菩萨!
谢谢!
谢谢!
CrazyFor 2003-08-22
  • 打赏
  • 举报
回复
select * from 表 a where (select count(*) from 表 b where a.bb=b.bb and b.cc<=a.cc)<=5
老宛 2003-08-22
  • 打赏
  • 举报
回复
use 数据库
select aa,bb,cc into #q from 表
group by bb,aa,cc
order by bb,cc desc
declare wys cursor for
select * from #q
open wys
declare @i int,@j int,@x int,@t nvarchar(10),@a int,@b nvarchar(10),@c datetime
select @i=1,@j=1,@x=(select count(*) from #q)
fetch next from wys into @a,@b,@c
set @t=@b
while @i<=@x
begin
if @t<>@b
select @t=@b,@j=1
if @t=@b and @j>5
delete from #q where current of wys
fetch next from wys into @a,@b,@c
select @i=@i+1, @j=@j+1
end
close wys
deallocate wys
select * from #q

写的太繁琐了,哪位高人能写一个简练的,请不吝赐教

34,575

社区成员

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

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