请问返回行号的函数有吗

wzj1321 2007-07-09 09:42:41
如表 T_1 中有字段 A,B 假设数据如下
A B
王 12
李 10
王 20
张 30
我想返回 1 王 12
2 王 20
3 王(小计) 32
4 李 10
5 李(小计) 10
6 张 30
7 张(小计) 30 用 union all 来实现可以吗???
...全文
436 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzj1321 2007-07-10
  • 打赏
  • 举报
回复
但必须用select 得出啊
wzj1321 2007-07-10
  • 打赏
  • 举报
回复
其他结果不要也没关系,只要得总的行数(n返回结果为 1,2,3,4.....n
wzj1321 2007-07-10
  • 打赏
  • 举报
回复
1 李 10
2 李 10
1 王 20
2 王 12
3 王 32
1 张 30
2 张 30
返回结果不对啊,前面ID应该是连续的
leo_lesley 2007-07-10
  • 打赏
  • 举报
回复
declare @t table(a varchar(10),b int)
insert into @t
select '王',12 union all
select '李',10 union all
select '王',20 union all
select '张',30


select * from (
select id=count(a)+1,a=a+'(小计)',b=sum(b) from @t group by a
union all
select id=(select count(1) from @t where t.a=a and t.b<=b),*
from @t t) d
order by a,id
leo_lesley 2007-07-10
  • 打赏
  • 举报
回复
declare @t table(a varchar(10),b int)
insert into @t
select '王',12 union all
select '李',10 union all
select '王',20 union all
select '张',30

select id=count(a)+1,a,c=sum(b) from @t group by a
union all
select id=(select count(1) from @t where t.a=a and t.b<=b),*
from @t t
order by a,id
wzj1321 2007-07-10
  • 打赏
  • 举报
回复
感谢各位,不过能不能不用新建表或存储过程,直接用select 来产生呢???
simonhehe 2007-07-09
  • 打赏
  • 举报
回复
declare @t table(a varchar(10),b int)
declare @tt table(id int identity(1,1),a varchar(10),b int)
insert into @t
select '王',12 union all
select '李',10 union all
select '王',20 union all
select '张',30

insert into @tt
select a= case when grouping(b)=1 then a + '(小计)' else a end,b = sum(b)
from @t
group by a,b with rollup
having grouping(a)=0

select * from @tt
bill024 2007-07-09
  • 打赏
  • 举报
回复
--用union all, 改下

create table test(A varchar(10),B int)
insert test select '王',12
union all select '李',10
union all select '王',20
union all select '张',30

create table tmp(id int identity(1,1),A varchar(10),B int)
go

insert tmp(A,B)
select A,B from
(
select A,B,s2=A,s3=0 from test
union all
select A+'(小计)',sum(B),s2=A,s3=1 from test
group by A
)a
order by s2,s3

select * from tmp

drop table test,tmp

id A B
----------- ---------- -----------
1 李 10
2 李(小计) 10
3 王 12
4 王 20
5 王(小计) 32
6 张 30
7 张(小计) 30

(所影响的行数为 7 行)
simonhehe 2007-07-09
  • 打赏
  • 举报
回复
--忘了lz不要合计值,改一下
--主要在 with rollup

declare @t table(a varchar(10),b int)
insert into @t
select '王',12 union all
select '李',10 union all
select '王',20 union all
select '张',30

select a= case when grouping(b)=1 then a + '(小计)' else a end,b = sum(b)
from @t
group by a,b with rollup
having grouping(a)=0
bill024 2007-07-09
  • 打赏
  • 举报
回复
create table test(A varchar(10),B int)
insert test select '王',12
union all select '李',10
union all select '王',20
union all select '张',30


select A,B from
(
select A,B,s1=0,s2=A,s3=0 from test
union all
select A+'(小计)',sum(B),s1=0,s2=A,s3=1 from test
group by A
)a
order by s1,s2,s3

drop table test

A B
---------------- -----------
李 10
李(小计) 10
王 12
王 20
王(小计) 32
张 30
张(小计) 30
simonhehe 2007-07-09
  • 打赏
  • 举报
回复
declare @t table(a varchar(10),b int)
insert into @t
select '王',12 union all
select '李',10 union all
select '王',20 union all
select '张',30

select a= case when grouping(b)=1 then a + '(小计)'else a end,b = sum(b)
from @t
group by a,b with rollup
having grouping(a)=0 or grouping(b)=1
wzj1321 2007-07-09
  • 打赏
  • 举报
回复
行不行啊,没有这样的函数的话,请大家也给个回复啊,好让我想想其他办法

34,590

社区成员

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

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