表内容:
Sdate F1 F2 Name1
20041101 5 7 a
20041101 2 3 b
20041101 33 4 c
20041101 1 8 d
20041102 12 1 a
... ...
现在想实现显示:
20041101日统计
5 7 a
2 3 b
33 4 c
1 8 d
41 22 总计
20041102日统计
... ...
日统计,怎么做?
...全文
23317打赏收藏
日统计输出
表内容: Sdate F1 F2 Name1 20041101 5 7 a 20041101 2 3 b 20041101 33 4 c 20041101 1 8 d 20041102 12 1 a ... ... 现在想实现显示: 20041101日统计 5 7 a 2 3 b 33 4 c 1 8 d 41 22 总计 20041102日统计 ... ... 日统计,怎么做?
select Sdate,F1,F2,Name1
from (select Sdate,sum(F1),sum(F2),Name1
from table
group by sdate,name1
union all
select Sdate,Sum(F1),sum(F2),'总计' as name1
from table
group by sdate ) a
order by sdate
insert ##test
select '20041101','5' , '7', 'a'
union all select '20041101', '2', '3', 'b'
union all select '20041101', '33', '4', 'c'
union all select '20041101', '1', '8', 'd'
union all select '20041102', '12', '1', 'a'
若你的总计意思为 sum(f1)+sum(f2)总和的话,则用下面这个:
select * from(
select * from ##test
union all
select sdate,sum(f1),sum(f2),cast(sum(f1)+sum(f2) as varchar) as 总计 from ##test group by sdate) a order by a.sdate
----结果
20041101 5 7 a
20041101 2 3 b
20041101 33 4 c
20041101 1 8 d
20041101 41 22 63
20041102 12 1 a
20041102 12 1 13
--=============================================
若你的总计意思只是在name1列显示文字“总计”的话,则用这个:
select * from(
select * from ##test
union all
select sdate,sum(f1),sum(f2), '总计' from ##test group by sdate) a order by a.sdate
--结果:
20041101 5 7 a
20041101 2 3 b
20041101 33 4 c
20041101 1 8 d
20041101 41 22 总计
20041102 12 1 a
20041102 12 1 总计