目前有一个汇总统计的应用,小弟想了很久,未果。请求各位大哥相助,谢谢大家。
CREATE TABLE tab1
(
arcID INT IDENTITY, --文章流水号
arcAuthor VARCHAR(20), --文章作者
arcColumn VARCHAR(10), --栏目
arcColScore INT , --每个栏目的基本分值(评分标准)
arcNum VARCHAR(10), --期数
)INSERT into tab1 (arcAuthor,arcColumn,arcColScore,arcNum)
SELECT '张','2_体育新闻',3,'6-10' UNION ALL
SELECT '张','1_科技新闻',5,'6-10' UNION ALL
SELECT '吴','1_科技新闻',5,'6-10' UNION ALL
SELECT '林','3_娱乐新闻',2,'6-10' UNION ALL
SELECT '张','1_科技新闻',5,'7-11' UNION ALL
SELECT '张','1_科技新闻',5,'8-12' 现在想按统计各个栏目被采用的期次(被采用次数)、栏目总分和各个栏目总得分,报表如下(XLS附件传不上去啊)
1_科技新闻 2_体育新闻 3_娱乐新闻 总得分
人员 期次 分值 总分 期次 分值 总分 期次 分值 总分
张 3 5 15 1 3 3 0 2 0 18
吴 1 5 5 0 3 0 0 2 0 5
林 0 5 0 0 3 0 1 2 2 2
CREATE TABLE tab1
(
arcID INT IDENTITY, --文章流水号
arcAuthor VARCHAR(20), --文章作者
arcColumn VARCHAR(10), --栏目
arcColScore INT , --每个栏目的基本分值(评分标准)
arcNum VARCHAR(10), --期数
)INSERT into tab1 (arcAuthor,arcColumn,arcColScore,arcNum)
SELECT '张','2_体育新闻',3,'6-10' UNION ALL
SELECT '张','1_科技新闻',5,'6-10' UNION ALL
SELECT '吴','1_科技新闻',5,'6-10' UNION ALL
SELECT '林','3_娱乐新闻',2,'6-10' UNION ALL
SELECT '张','1_科技新闻',5,'7-11' UNION ALL
SELECT '张','1_科技新闻',5,'8-12' 现在想按统计各个栏目被采用的期次(被采用次数)、栏目总分和各个栏目总得分,报表如下(XLS附件传不上去啊)
1_科技新闻 2_体育新闻 3_娱乐新闻 总得分
人员 期次 分值 总分 期次 分值 总分 期次 分值 总分
张 3 5 15 1 3 3 0 2 0 18
吴 1 5 5 0 3 0 0 2 0 5
林 0 5 0 0 3 0 1 2 2 2
go
CREATE TABLE tab1
(
arcID INT IDENTITY, --文章流水号
arcAuthor VARCHAR(20), --文章作者
arcColumn VARCHAR(10), --栏目
arcColScore INT , --每个栏目的基本分值(评分标准)
arcNum VARCHAR(10), --期数
)
INSERT into tab1 (arcAuthor,arcColumn,arcColScore,arcNum)
SELECT '张','2_体育新闻',3,'6-10' UNION ALL
SELECT '张','1_科技新闻',5,'6-10' UNION ALL
SELECT '吴','1_科技新闻',5,'6-10' UNION ALL
SELECT '林','3_娱乐新闻',2,'6-10' UNION ALL
SELECT '张','1_科技新闻',5,'7-11' UNION ALL
SELECT '张','1_科技新闻',5,'8-12'
goselect
arcauthor 人员,
sum(case arcColumn when '1_科技新闻' then 1 else 0 end) [1_科技新闻/期次],
(select max(case arcColumn when '1_科技新闻' then arcColScore else 0 end) from tab1) [1_科技新闻/分值],
sum(case arcColumn when '1_科技新闻' then arcColScore else 0 end) [1_科技新闻/总分],
sum(case arcColumn when '2_体育新闻' then 1 else 0 end) [2_体育新闻/期次],
(select max(case arcColumn when '2_体育新闻' then arcColScore else 0 end) from tab1) [2_体育新闻/分值],
sum(case arcColumn when '2_体育新闻' then arcColScore else 0 end) [2_体育新闻/总分],
sum(case arcColumn when '3_娱乐新闻' then 1 else 0 end) [3_娱乐新闻/期次],
(select max(case arcColumn when '3_娱乐新闻' then arcColScore else 0 end) from tab1) [3_娱乐新闻/分值],
sum(case arcColumn when '3_娱乐新闻' then arcColScore else 0 end) [3_娱乐新闻/总分],
sum(arcColScore) 总分
from tab1
group by arcauthor
order by 人员 desc
/*
人员 1_科技新闻/期次 1_科技新闻/分值 1_科技新闻/总分 2_体育新闻/期次 2_体育新闻/分值 2_体育新闻/总分 3_娱乐新闻/期次 3_娱乐新闻/分值 3_娱乐新闻/总分 总分
-------------------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
张 3 5 15 1 3 3 0 2 0 18
吴 1 5 5 0 3 0 0 2 0 5
林 0 5 0 0 3 0 1 2 2 2(3 行受影响)
*/
标题:普通行列转换(version 2.0)
作者:爱新觉罗.毓华
时间:2008-03-09
地点:广东深圳
说明:普通行列转换(version 1.0)仅针对sql server 2000提供静态和动态写法,version 2.0增加sql server 2005的有关写法。问题:假设有张学生成绩表(tb)如下:
姓名 课程 分数
张三 语文 74
张三 数学 83
张三 物理 93
李四 语文 74
李四 数学 84
李四 物理 94
想变成(得到如下结果):
姓名 语文 数学 物理
---- ---- ---- ----
李四 74 84 94
张三 74 83 93
-------------------
*/create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
go--SQL SERVER 2000 静态SQL,指课程只有语文、数学、物理这三门课程。(以下同)
select 姓名 as 姓名 ,
max(case 课程 when '语文' then 分数 else 0 end) 语文,
max(case 课程 when '数学' then 分数 else 0 end) 数学,
max(case 课程 when '物理' then 分数 else 0 end) 物理
from tb
group by 姓名--SQL SERVER 2000 动态SQL,指课程不止语文、数学、物理这三门课程。(以下同)
declare @sql varchar(8000)
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
from (select distinct 课程 from tb) as a
set @sql = @sql + ' from tb group by 姓名'
exec(@sql) --SQL SERVER 2005 静态SQL。
select * from (select * from tb) a pivot (max(分数) for 课程 in (语文,数学,物理)) b--SQL SERVER 2005 动态SQL。
declare @sql varchar(8000)
select @sql = isnull(@sql + ',' , '') + 课程 from tb group by 课程
exec ('select * from (select * from tb) a pivot (max(分数) for 课程 in (' + @sql + ')) b')---------------------------------/*
问题:在上述结果的基础上加平均分,总分,得到如下结果:
姓名 语文 数学 物理 平均分 总分
---- ---- ---- ---- ------ ----
李四 74 84 94 84.00 252
张三 74 83 93 83.33 250
*/--SQL SERVER 2000 静态SQL。
select 姓名 姓名,
max(case 课程 when '语文' then 分数 else 0 end) 语文,
max(case 课程 when '数学' then 分数 else 0 end) 数学,
max(case 课程 when '物理' then 分数 else 0 end) 物理,
cast(avg(分数*1.0) as decimal(18,2)) 平均分,
sum(分数) 总分
from tb
group by 姓名
from tab1 group by arcAuthor,arcColumn
order by arcAuthor
select
arcauthor 人员,
sum(case arcColumn when '1_科技新闻' then 1 else 0 end) [1_科技新闻/期次],
(select max(case arcColumn when '1_科技新闻' then arcColScore else 0 end) from tab1) [1_科技新闻/分值],
sum(case arcColumn when '1_科技新闻' then arcColScore else 0 end) [1_科技新闻/总分],
sum(case arcColumn when '2_体育新闻' then 1 else 0 end) [2_体育新闻/期次],
(select max(case arcColumn when '2_体育新闻' then arcColScore else 0 end) from tab1) [2_体育新闻/分值],
sum(case arcColumn when '2_体育新闻' then arcColScore else 0 end) [2_体育新闻/总分],
sum(case arcColumn when '3_娱乐新闻' then 1 else 0 end) [3_娱乐新闻/期次],
(select max(case arcColumn when '3_娱乐新闻' then arcColScore else 0 end) from tab1) [3_娱乐新闻/分值],
sum(case arcColumn when '3_娱乐新闻' then arcColScore else 0 end) [3_娱乐新闻/总分],
sum(arcColScore) 总分
from tab1
group by arcauthor
order by 人员 desc
CREATE PROCEDURE PROC_Test
AS
select arcAuthor,arcColumn,b.times,name='times' from (select arcAuthor,arcColumn,count(arcColumn) as times,arcColScore,sum(arcColScore) as Quaty from tab1 group by arcColumn,arcAuthor,arcColScore) b
union
select arcAuthor,arcColumn,b.arcColScore,name='arcColScore' from (select arcAuthor,arcColumn,count(arcColumn) as times,arcColScore,sum(arcColScore) as Quaty from tab1 group by arcColumn,arcAuthor,arcColScore) b
union
select arcAuthor,arcColumn,b.Quaty,name='Quaty' from (select arcAuthor,arcColumn,count(arcColumn) as times,arcColScore,sum(arcColScore) as Quaty from tab1 group by arcColumn,arcAuthor,arcColScore) b
GO/*
--exec PROC_Test
结果
林 3_娱乐新闻 1 times
林 3_娱乐新闻 2 arcColScore
林 3_娱乐新闻 2 Quaty
吴 1_科技新闻 1 times
吴 1_科技新闻 5 arcColScore
吴 1_科技新闻 5 Quaty
张 1_科技新闻 3 times
张 1_科技新闻 5 arcColScore
张 1_科技新闻 15 Quaty
张 2_体育新闻 1 times
张 2_体育新闻 3 arcColScore
张 2_体育新闻 3 Quaty
*/
数据集设计
数据集名 ds_test
表名 TB