求一条SQL语句

wobencm 2010-02-05 06:49:37
表信息如下
a0188 id jlh
4343 NULL 157509
4344 NULL 5472
4344 NULL 5473
4344 NULL 5474
4344 NULL 5476
4344 NULL 10450
4344 NULL 18096
4344 NULL 131946
4345 NULL 2507
4345 NULL 2509
4345 NULL 123075
4345 NULL 125824
4345 NULL 147097
4345 NULL 157325
4345 NULL 157326
4346 NULL 2511
4346 NULL 18220
4346 NULL 148259
想给ID排序按照 a0188组且按照JLH大小顺序排
想得到的结果如下
a0188 id jlh
4343 1 157509
4344 1 5472
4344 2 5473
4344 3 5474
4344 4 5476
4344 5 10450
4344 6 18096
4344 7 131946
4345 1 2507
4345 2 2509
4345 3 123075
4345 4 125824
4345 5 147097
4345 6 157325
4345 7 157326
4346 1 2511
4346 2 18220
4346 3 148259

...全文
95 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wobencm 2010-02-05
  • 打赏
  • 举报
回复
谢谢,结贴。
ws_hgo 2010-02-05
  • 打赏
  • 举报
回复
写错了个
小地方

create table #TT
(
a0188 int,
id nchar(10),
jlh int
)
insert into #TT select 4343,null,157509
insert into #TT select 4344,null,5472
insert into #TT select 4344,null,5473
insert into #TT select 4344,null,5474
insert into #TT select 4344,null,5476
insert into #TT select 4344,null,10450
insert into #TT select 4344,null,18096
insert into #TT select 4344,null,131946
insert into #TT select 4345,null,2507
insert into #TT select 4345,null,2509
insert into #TT select 4345,null,123075
insert into #TT select 4345,null,125824
insert into #TT select 4345,null,147097
insert into #TT select 4345,null,157325
insert into #TT select 4345,null,157326
insert into #TT select 4346,null,2511
insert into #TT select 4346,null,18220
insert into #TT select 4346,null,148259

select a0188,
(select count(*)+1 from #TT where a0188=T.a0188 and jlh<T.jlh) ID,
jlh
from #TT T
order by a0188 asc,ID asc,jlh desc

a0188 ID jlh
----------- ----------- -----------
4343 1 157509
4344 1 5472
4344 2 5473
4344 3 5474
4344 4 5476
4344 5 10450
4344 6 18096
4344 7 131946
4345 1 2507
4345 2 2509
4345 3 123075
4345 4 125824
4345 5 147097
4345 6 157325
4345 7 157326
4346 1 2511
4346 2 18220
4346 3 148259
wobencm 2010-02-05
  • 打赏
  • 举报
回复
谢谢,我去试下,可以马上结贴
ws_hgo 2010-02-05
  • 打赏
  • 举报
回复
create table #TT
(
a0188 int,
id nchar(10),
jlh int
)
insert into #TT select 4343,null,157509
insert into #TT select 4344,null,5472
insert into #TT select 4344,null,5473
insert into #TT select 4344,null,5474
insert into #TT select 4344,null,5476
insert into #TT select 4344,null,10450
insert into #TT select 4344,null,18096
insert into #TT select 4344,null,131946
insert into #TT select 4345,null,2507
insert into #TT select 4345,null,2509
insert into #TT select 4345,null,123075
insert into #TT select 4345,null,125824
insert into #TT select 4345,null,147097
insert into #TT select 4345,null,157325
insert into #TT select 4345,null,157326
insert into #TT select 4346,null,2511
insert into #TT select 4346,null,18220
insert into #TT select 4346,null,148259

select a0188,
(select count(*)+1 from #TT where a0188=T.a0188 and jlh>T.jlh) ID,
jlh
from #TT T
order by a0188 asc,ID asc


a0188 ID jlh
----------- ----------- -----------
4343 1 157509
4344 1 131946
4344 2 18096
4344 3 10450
4344 4 5476
4344 5 5474
4344 6 5473
4344 7 5472
4345 1 157326
4345 2 157325
4345 3 147097
4345 4 125824
4345 5 123075
4345 6 2509
4345 7 2507
4346 1 148259
4346 2 18220
4346 3 2511

(18 行受影响)
百年树人 2010-02-05
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a0188] int,[id] sql_variant,[jlh] int)
insert [tb]
select 4343,null,157509 union all
select 4344,null,5472 union all
select 4344,null,5473 union all
select 4344,null,5474 union all
select 4344,null,5476 union all
select 4344,null,10450 union all
select 4344,null,18096 union all
select 4344,null,131946 union all
select 4345,null,2507 union all
select 4345,null,2509 union all
select 4345,null,123075 union all
select 4345,null,125824 union all
select 4345,null,147097 union all
select 4345,null,157325 union all
select 4345,null,157326 union all
select 4346,null,2511 union all
select 4346,null,18220 union all
select 4346,null,148259

select [a0188],
id=(select count(1)+1 from tb where [a0188]=t.[a0188] and JLH<t.JLH),
JLH
from tb t
order by 1,2,3


--测试结果:
/*
a0188 id JLH
----------- ----------- -----------
4343 1 157509
4344 1 5472
4344 2 5473
4344 3 5474
4344 4 5476
4344 5 10450
4344 6 18096
4344 7 131946
4345 1 2507
4345 2 2509
4345 3 123075
4345 4 125824
4345 5 147097
4345 6 157325
4345 7 157326
4346 1 2511
4346 2 18220
4346 3 148259

(所影响的行数为 18 行)

*/
百年树人 2010-02-05
  • 打赏
  • 举报
回复
select [a0188],
id=(select count(1)+1 from tb where [a0188]=t.[a0188] and JLH<t.JLH)
JLH
from tb t
order by 1,2,3
wobencm 2010-02-05
  • 打赏
  • 举报
回复
就是要生成ID序号
wobencm 2010-02-05
  • 打赏
  • 举报
回复
楼上的没看懂我的意思啊。我想得到的表结果你可以看下!
不过还是很谢谢楼上的
ws_hgo 2010-02-05
  • 打赏
  • 举报
回复
你的id为null吗
ws_hgo 2010-02-05
  • 打赏
  • 举报
回复
select * from tb order by a0188 asc,id desc,JLH desc

34,590

社区成员

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

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