表1如下:列1 列2 列3 列4
1 供应商 华南师范 3123.22
1 26589.34
2 客户 北京航空 1236.35
2 45696.63
2 56986.32
3 639.65
3 1698.56
3 客户 大连理工 5698.63sql查询成如下表2:1 供应商 华南师范 3123.22
1 供应商 华南师范 26589.34
1 合计 单数2 29712.562 客户 北京航空 1236.35
2 客户 北京航空 45696.63
2 客户 北京航空 56986.32
2 合计 单数3 103919.303 客户 大连理工 639.65
3 客户 大连理工 1698.56
3 客户 大连理工 5698.63
3 合计 单数3 8036.84 总计 单数8 141668.70 拜求各位高手赐教,谢谢!!!
1 供应商 华南师范 3123.22
1 26589.34
2 客户 北京航空 1236.35
2 45696.63
2 56986.32
3 639.65
3 1698.56
3 客户 大连理工 5698.63sql查询成如下表2:1 供应商 华南师范 3123.22
1 供应商 华南师范 26589.34
1 合计 单数2 29712.562 客户 北京航空 1236.35
2 客户 北京航空 45696.63
2 客户 北京航空 56986.32
2 合计 单数3 103919.303 客户 大连理工 639.65
3 客户 大连理工 1698.56
3 客户 大连理工 5698.63
3 合计 单数3 8036.84 总计 单数8 141668.70 拜求各位高手赐教,谢谢!!!
if object_id('tempdb.dbo.#') is not null drop table #
create table #(c1 int, c2 varchar(8), c3 varchar(8), c4 float)
insert into #
select 1, '供应商', '华南师范', 3123.22 union all
select 1, null, null, 26589.34 union all
select 2, '客户', '北京航空', 1236.35 union all
select 2, null, null, 45696.63 union all
select 2, null, null, 56986.32 union all
select 3, null, null, 639.65 union all
select 3, null, null, 1698.56 union all
select 3, '客户', '大连理工', 5698.63select
a.c1,isnull(a.c2,b.c2)c2,isnull(a.c3,b.c3)c3, c4
from
# a inner join (select distinct c1,c2,c3 from # where c2 is not null) b on a.c1=b.c1
UNION ALL
select c1, '合计', '单数'+ltrim(count(1)), sum(c4) from # group by c1
order by c1, c4/*
c1 c2 c3 c4
----------- -------- ---------------- ----------------------
1 供应商 华南师范 3123.22
1 供应商 华南师范 26589.34
1 合计 单数2 29712.56
2 客户 北京航空 1236.35
2 客户 北京航空 45696.63
2 客户 北京航空 56986.32
2 合计 单数3 103919.3
3 客户 大连理工 639.65
3 客户 大连理工 1698.56
3 客户 大连理工 5698.63
3 合计 单数3 8036.84
*/
if object_id('tempdb.dbo.#') is not null drop table #
create table #(c1 int, c2 varchar(8), c3 varchar(8), c4 float)
insert into #
select 1, '供应商', '华南师范', 3123.22 union all
select 1, '', '', 26589.34 union all
select 2, '客户', '北京航空', 1236.35 union all
select 2, '', '', 45696.63 union all
select 2, '', '', 56986.32 union all
select 3, '', '', 639.65 union all
select 3, '', '', 1698.56 union all
select 3, '客户', '大连理工', 5698.63
go;with josy as
(
select
c1,
c2=(select top 1 c2 from # where c1=t.c1 order by c2 desc),
c3=(select top 1 c3 from # where c1=t.c1 order by c3 desc),
c4
from # t
)
select c1,case when c1 is null then '总计' else c2 end as c2,c3,c4
from(
select * from josy
union all
select c1, '合计', '单数'+ltrim(count(1)), sum(c4) from # group by c1 with rollup
) t
order by
case when c1 is null then 2 else 1 end,
c1,
case when c2='合计' then 2 else 1 end/**
c1 c2 c3 c4
----------- -------- ---------------- ----------------------
1 供应商 华南师范 3123.22
1 供应商 华南师范 26589.34
1 合计 单数2 29712.56
2 客户 北京航空 1236.35
2 客户 北京航空 45696.63
2 客户 北京航空 56986.32
2 合计 单数3 103919.3
3 客户 大连理工 639.65
3 客户 大连理工 1698.56
3 客户 大连理工 5698.63
3 合计 单数3 8036.84
NULL 总计 单数8 141668.7
**/
我用sql2000查询分析器测试结果如下(已将#替换成表名):
SQLCenter:服务器: 消息 209,级别 16,状态 1,行 1
列名 'c1' 不明确。josy:服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: ';' 附近有语法错误。去掉with前的“;”后
服务器: 消息 156,级别 15,状态 1,行 1
在关键字 'with' 附近有语法错误。我是新手,还望两位高手赐教!非常感谢!
;with cte as
(
)这是2005的新特性。
另外SQLCenter的貌似没有问题。请LZ再确认一下。