日期:2014-05-19  浏览次数:20477 次

用一个SELECT 实现
例如t1为:
a                 b
qwe             1
dsfg           2
小计1         18
ger             19  
kuyre         20
we34           23
小计2         25
db               26
tyuq           29
小计3         35
能不能用一条SELECT语句实现
---
a                 b                     c
qwe             1                   18
dsfg           2                   18
小计1         18                 18
ger             19                 25
kuyre         20                 25
we34           23                 25
小计2         25                 25
db               26                 35
tyuq           29                 35
小计3         35                 35
其中b为升序排列



------解决方案--------------------
bu
------解决方案--------------------
动态sql
------解决方案--------------------
把你的查询语句帖出来!
在查询字段里加(select sum(num) from table where a.b=b and a.a=a ) as c
------解决方案--------------------
得到的C这一列是干什么用的?怎么没有看懂你要干什么啊?
------解决方案--------------------
create table dbo.one
(
id int identity(1,1),
a nvarchar(10),
b int
)

insert into one
select 'qwe ', 1 union all
select 'dsfg ', 2 union all
select N '小计1 ', 18 union all
select 'ger ', 19 union all
select 'kuyre ', 20 union all
select 'we34 ', 23 union all
select N '小计2 ', 25 union all
select 'db ', 26 union all
select 'tyuq ', 29 union all
select N '小计3 ', 35

select y.oid as yid, x.oid as xid, x.b
from
(
select min(id) as oid, convert(int, replace(a, N '小计 ', ' ')) as id, min(b) as b
from one group by a having substring(a, 1,2) = N '小计 '
) x
inner join
(
select min(id) as oid, convert(int, replace(a, N '小计 ', ' ')) as id, min(b) as b
from one group by a having substring(a, 1,2) = N '小计 '
) y
on x.id = y.id + 1

union all

select 1 as oid, oid as id, b
from
(
select top 1 min(id) as oid, min(b) as b
from one group by a having substring(a, 1,2) = N '小计 '
) z

drop table one