34,594
社区成员
发帖
与我相关
我的任务
分享
---测试数据---
declare @tb table(柜台号 int,单号 varchar(5))
insert @tb
select 1, 'A' union all
select 1, 'B' union all
select 1, 'C' union all
select 2, 'D' union all
select 2, 'E' union all
select 3, 'F'
---查询---
select
柜台号,
单号,
标号=(select count(1) from @tb where 柜台号=a.柜台号 and 单号<=a.单号)
from @tb a
---结果---
柜台号 单号 标号
----------- ----- -----------
1 A 1
1 B 2
1 C 3
2 D 1
2 E 2
3 F 1
(所影响的行数为 6 行)
create table tb(柜台号 int, 单号 varchar(10))
insert tb select 1 , 'A'
insert tb select 1 , 'B'
insert tb select 1 , 'C'
insert tb select 2 , 'D'
insert tb select 2 , 'E'
insert tb select 3, 'F'
select * , 标号 = (select count(1) from tb where 柜台号 = t.柜台号 and 单号 < t.单号) + 1 from tb t
drop table tb
/*
柜台号 单号 标号
----------- ---------- -----------
1 A 1
1 B 2
1 C 3
2 D 1
2 E 2
3 F 1
(所影响的行数为 6 行)
*/
select * , 标号 = (select count(1) from tb where 柜台号 = t.柜台号 and 单号 < t.单号) + 1 from tb
declare @t table(柜台号 int, 单号 varchar(10))
insert @t select 1 , 'A'
insert @t select 1 , 'B'
insert @t select 1 , 'C'
insert @t select 2 , 'D'
insert @t select 2 , 'E'
insert @t select 3, 'F'
select * ,cnt=(select count(*) from @t where 柜台号=t.柜台号 and 单号<t.单号)+1 from @t t
/*柜台号 单号 cnt
----------- ---------- -----------
1 A 1
1 B 2
1 C 3
2 D 1
2 E 2
3 F 1
(影響 6 個資料列)*/
--> By dobear_0922(小熊) 2008-11-28 10:57:17
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([柜台号] int,[单号] varchar(1))
insert [tb]
select 1,'A' union all
select 1,'B' union all
select 1,'C' union all
select 2,'D' union all
select 2,'E' union all
select 3,'F'
select 柜台号, 单号
, 标号=(select count(*) from tb where 柜台号=t.柜台号 and 单号<=t.单号)
from tb t
/*
柜台号 单号 标号
----------- ---- -----------
1 A 1
1 B 2
1 C 3
2 D 1
2 E 2
3 F 1
(6 行受影响)
*/
drop table [tb]
select 柜台号, 单号
, 标号=(select count(*) from tb where 柜台号=t.柜台号 and 单号<=t.单号)
from tb t
select *,标号=(select count(1)+1 from [Table] where 柜台号=a.柜台号 and 单号<a.单号) from [Table] a