为符合客户的打印要求,求SQL语句
表如下
id name 序号 姓名
1 张三
2 张三
3 张三
4 张三
5 李四
6 李四
7 李四
8 王二
通过SQL转化为:
id name 序号 姓名
1 张三 1 张三
2 张三
3 张三
4 张三
5 李四 2 李四
6 李四
7 李四
8 王二 3 王二
[解决办法]
create table T(id int, name varchar(10),序号 varchar(10),姓名 varchar(10))
insert T select 1, '张三 ', ' ', ' '
insert T select 2, '张三 ', ' ', ' '
insert T select 3, '张三 ', ' ', ' '
insert T select 4, '张三 ', ' ', ' '
insert T select 5, '李四 ', ' ', ' '
insert T select 6, '李四 ', ' ', ' '
insert T select 7, '李四 ', ' ', ' '
insert T select 8, '王二 ', ' ', ' '
update T
set
序号=(select count(distinct name)+1 from T where id <a.id and name!=a.name),
姓名=name
from T a
where
not exists(select 1 from T where name=a.name and id <a.id)
select * from T
drop table T
--结果
id name 序号 姓名
----------- ---------- ---------- ----------
1 张三 1 张三
2 张三
3 张三
4 张三
5 李四 2 李四
6 李四
7 李四
8 王二 3 王二
(所影响的行数为 8 行)
[解决办法]
declare @T table (id int, name varchar(10),序号 varchar(10),姓名 varchar(10))
insert @T select 1, '张三 ', ' ', ' '
insert @T select 2, '张三 ', ' ', ' '
insert @T select 3, '张三 ', ' ', ' '
insert @T select 4, '张三 ', ' ', ' '
insert @T select 5, '李四 ', ' ', ' '
insert @T select 6, '李四 ', ' ', ' '
insert @T select 7, '李四 ', ' ', ' '
insert @T select 8, '王二 ', ' ', ' '
update @T
set 序号=b.id,
姓名=b.name
from @T a,
(select * from @T where id in(select min(id) from @T group by name)) b
where a.id=b.id
select * from @t
----result
1张三1张三
2张三
3张三
4张三
5李四5李四
6李四
7李四
8王二8王二
[解决办法]
create table ttt(id int, name nvarchar(10))
insert ttt
select 1, N '张三 '
union select 2 ,N '张三 '
union select 3 ,N '张三 '
union select 4 ,N '张三 '
union select 5 ,N '李四 '
union select 6 ,N '李四 '
union select 7 ,N '李四 '
union select 8 ,N '王二 '
select [id],[name],[id]=case when (select count(1) from ttt where id <a.id and name=a.name)=0 then [id] else null end,[name]=case when (select count(1) from ttt where id <a.id and name=a.name)=0 then [name] else ' ' end from ttt a
drop table ttt
[解决办法]
create table t
(id smallint, name varchar(10) )--序號 姓名
insert into t
select 1, '張三 ' union all
select 2, '張三 ' union all
select 3, '張三 ' union all
select 4, '張三 ' union all
select 5, '李四 ' union all
select 6, '李四 ' union all
select 7, '李四 ' union all
select 8, '王二 '
select a.id,a.name,b.序號, b.姓名 from t a
left join
(
select name as 姓名,min(id)as id, min(id/3)+1 as 序號 from t group by name
)b
on a.id=b.id and a.name=姓名
id name 序號 姓名
------ ---------- ----------- ----------
1 張三 1 張三
2 張三 NULL NULL
3 張三 NULL NULL
4 張三 NULL NULL
5 李四 2 李四
6 李四 NULL NULL
7 李四 NULL NULL
8 王二 3 王二
(8 row(s) affected)
[解决办法]
select identity(int,1,1) x,min(id) id into #temp_t from t group by name
update t set 序号=#temp.x and 姓名=t.name from #temp where t.id=#temp.id
drop table #temp
[解决办法]
create table T(id int, name varchar(10),序号 varchar(10),姓名 varchar(10))
insert T select 1, '张三 ', ' ', ' '
insert T select 2, '张三 ', ' ', ' '
insert T select 3, '张三 ', ' ', ' '
insert T select 4, '张三 ', ' ', ' '
insert T select 5, '李四 ', ' ', ' '
insert T select 6, '李四 ', ' ', ' '
insert T select 7, '李四 ', ' ', ' '
insert T select 8, '王二 ', ' ', ' '
update t set 序号=a.id,姓名=a.name
from t ,
(select * from t a
where not exists(select 1 from t where name=a.name and a.id> id )) a
where t.id=a.id
select * from t
id name 序号 姓名
----------- ---------- ---------- ----------
1 张三 1 张三
2 张三
3 张三
4 张三
5 李四 5 李四
6 李四
7 李四
8 王二 8 王二
(所影响的行数为 8 行)