这种查询该如何写?

lhb2000 2007-11-08 08:53:05

ID LB MC JE SJ
1 AA 001 1 2007-10-1
2 AA 002 2 2007-10-2
3 BB 001 1 2007-10-5
4 CC 005 5 2007-10-7
5 CC 007 8 2007-10-8
6 DD 007 8 2007-10-11
....
如何得到如下的查询结果
1 AA 001 1 2007-10-1 1
2 AA 002 2 2007-10-2 1
3 BB 001 1 2007-10-5 2
4 CC 005 5 2007-10-7 3
5 CC 007 8 2007-10-8 3
6 DD 007 8 2007-10-11 4

就是得到LB不重复的排序
...全文
80 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhb2000 2007-11-08
  • 打赏
  • 举报
回复
用时间作条件时 当时间相同的结果就不对了
pt1314917 2007-11-08
  • 打赏
  • 举报
回复

declare @t table(id int,lb varchar(10),mc varchar(10),je int,sj datetime)
insert into @t select 1,'AA','001',1,'2007-10-1'
insert into @t select 2,'AA','002',2,'2007-10-2'
insert into @t select 3,'BB','001',1,'2007-10-5'
insert into @t select 4,'CC','005',5,'2007-10-7'
insert into @t select 5,'CC','007',8,'2007-10-8'
insert into @t select 6,'DD','007',8,'2007-10-11'

select bh=identity(int,1,1),lb into #temp from @t group by lb
select a.*,b.bh from @t a,#temp b where a.lb=b.lb


lhb2000 2007-11-08
  • 打赏
  • 举报
回复
测试成功 结贴
liangCK 2007-11-08
  • 打赏
  • 举报
回复
declare @t table(ID int,LB varchar(10),MC varchar(10),JE int,SJ datetime)
insert @t select 1,'AA','001',1,'2007-10-1'
union all select 2,'AA','002',2,'2007-10-2'
union all select 3,'BB','001',1,'2007-10-5'
union all select 4,'CC','005',5,'2007-10-7'
union all select 5,'CC','007',8,'2007-10-8'
union all select 6,'DD','007',8,'2007-10-11'

select a.*,(select count(distinct LB) from @t where ID!>a.ID) from @t a


同意roy_88
-狙击手- 2007-11-08
  • 打赏
  • 举报
回复
declare @A table(id int,lb nvarchar(2),mc nvarchar(5),je int,sj varchar(10)) 
insert @a select 1,'AA','001',1,'2007-10-1'
insert @a select 2,'AA','002',2,'2007-10-2'
insert @a select 3,'BB','001',1,'2007-10-5'
insert @a select 4,'CC','005',5,'2007-10-7'
insert @a select 5,'CC','007',8,'2007-10-8'
insert @a select 6,'DD','007',8,'2007-10-11'


select
*,[order]=(select count(distinct lb) from @A where ID<=t.ID)
from
@A t
/*
id lb mc je sj order
----------- ---- ----- ----------- ---------- -----------
1 AA 001 1 2007-10-1 1
2 AA 002 2 2007-10-2 1
3 BB 001 1 2007-10-5 2
4 CC 005 5 2007-10-7 3
5 CC 007 8 2007-10-8 3
6 DD 007 8 2007-10-11 4
*/
中国风 2007-11-08
  • 打赏
  • 举报
回复
declare @t table(ID int,      LB  nvarchar(5),     MC  nvarchar(3),   JE  int,   SJ datetime)
insert @t select 1, 'AA', '001', 1, '2007-10-1'
insert @t select 2, 'AA', '002', 2, '2007-10-2'
insert @t select 3, 'BB', '001', 1, '2007-10-5'
insert @t select 4, 'CC', '005', 5, '2007-10-7'
insert @t select 5, 'CC', '007', 8, '2007-10-8'
insert @t select 6, 'DD', '007', 8, '2007-10-11'

select
*,
[Order]=(select count(distinct LB) from @T where SJ!>t.SJ)--换作时间做为条件
from
@t t


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)

ID LB MC JE SJ Order
----------- ----- ---- ----------- ------------------------------------------------------ -----------
1 AA 001 1 2007-10-01 00:00:00.000 1
2 AA 002 2 2007-10-02 00:00:00.000 1
3 BB 001 1 2007-10-05 00:00:00.000 2
4 CC 005 5 2007-10-07 00:00:00.000 3
5 CC 007 8 2007-10-08 00:00:00.000 3
6 DD 007 8 2007-10-11 00:00:00.000 4

(所影响的行数为 6 行)

中国风 2007-11-08
  • 打赏
  • 举报
回复
declare @t table(ID int,      LB  nvarchar(5),     MC  nvarchar(3),   JE  int,   SJ datetime)
insert @t select 1, 'AA', '001', 1, '2007-10-1'
insert @t select 2, 'AA', '002', 2, '2007-10-2'
insert @t select 3, 'BB', '001', 1, '2007-10-5'
insert @t select 4, 'CC', '005', 5, '2007-10-7'
insert @t select 5, 'CC', '007', 8, '2007-10-8'
insert @t select 6, 'DD', '007', 8, '2007-10-11'

select
*,
[Order]=(select count(distinct LB) from @T where LB!>t.LB)
from
@t t


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)

ID LB MC JE SJ Order
----------- ----- ---- ----------- ------------------------------------------------------ -----------
1 AA 001 1 2007-10-01 00:00:00.000 1
2 AA 002 2 2007-10-02 00:00:00.000 1
3 BB 001 1 2007-10-05 00:00:00.000 2
4 CC 005 5 2007-10-07 00:00:00.000 3
5 CC 007 8 2007-10-08 00:00:00.000 3
6 DD 007 8 2007-10-11 00:00:00.000 4

(所影响的行数为 6 行)

中国风 2007-11-08
  • 打赏
  • 举报
回复
select *,[排序]=(select count(distinct LB) from ta where LB!>t.LB)
from
ta t

34,587

社区成员

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

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