ID ttotal Duration pid area pic
1 878 219 0 Content er
2 1117 299 3 Center Pic 1
3 1437 159 3 Center Pic 1
4 1616 319 3 Center Pic 1
5 2094 40 3 Center Pic 1
6 2154 40 3 Center Pic 1
7 2254 279 3 Center Pic 1
8 2553 199 2 L2 Pic 1
9 2792 459 3 Center Pic 1
10 3271 618 3 Center Pic 1
11 3909 558 3 Center Pic 1
12 4706 299 3 Center Pic 1
13 5025 1176 3 Center Pic 1
14 6261 279 4 R2 Pic 1
15 6720 120 1 L1 Pic 1
16 6860 100 1 L1 Pic 1
17 6999 259 3 Center Pic 1
18 7278 259 3 Center Pic 1
19 7557 299 3 Center Pic 1
20 8116 60 3 Center Pic 1
21 8195 1077 3 Center Pic 1
22 9292 798 3 Center Pic 1
23 10109 279 3 Center Pic 1
24 10688 399 3 Center Pic 1
25 11106 159 0 Blank
26 11286 80 0 Blank
27 11385 718 0 Blank
28 12123 60 3 Center Pic 1
29 12203 458 3 Center Pic 1
30 12701 60 3 Center Pic 1
31 12801 179 4 R2 Pic 1
32 13020 498 3 Center Pic 1
33 13539 179 3 Center Pic 1
34 13778 40 3 Center Pic 1
35 14097 439 3 Center Pic 1
36 14556 797 3 Center Pic 1
37 15373 40 3 Center Pic 1
38 15473 279 0 Blank
39 15772 518 0 Content er
40 16469 40 8 Center Pic 2
41 16569 100 8 Center Pic 2
42 16689 1216 8 Center Pic 2
43 17965 179 9 R2 Pic 2
44 18164 279 8 Center Pic 2
45 18463 179 8 Center Pic 2
46 18663 518 8 Center Pic 2
47 19420 558 8 Center Pic 2
48 20258 518 8 Center Pic 2
49 20796 179 8 Center Pic 2
50 20995 1136 8 Center Pic 2
51 22192 100 7 L2 Pic 2
52 22331 738 8 Center Pic 2
53 23089 279 8 Center Pic 2
54 23388 478 8 Center Pic 2
55 23886 678 8 Center Pic 2
56 24803 837 8 Center Pic 2
57 25681 538 8 Center Pic 2
58 26239 160 8 Center Pic 2
59 26418 100 0 Blank
60 26538 219 0 Blank
61 26917 80 0 Blank
62 27017 279 0 Blank
63 27316 100 0 Blank
64 27435 299 8 Center Pic 2
65 27754 199 8 Center Pic 2
66 27974 259 8 Center Pic 2
67 28472 239 8 Center Pic 2
68 28731 80 8 Center Pic 2
69 29130 40 9 R2 Pic 2
70 29190 379 10 R1 Pic 2
71 29588 299 9 R2 Pic 2
72 29927 199 8 Center Pic 2
73 30147 40 8 Center Pic 2
74 30207 319 8 Center Pic 2
75 30545 159 8 Center Pic 2
76 30725 160 8 Center Pic 2
77 30904 120 0 Blank
78 31044 40 0 Blank
79 31104 219 0 Content er
80 31343 279 0 Content er
81 31961 80 13 Center Pic 3
82 32081 159 13 Center Pic 3
83 32260 678 13 Center Pic 3
84 32958 478 13 Center Pic 3
85 33456 399 13 Center Pic 3
86 34074 1336 13 Center Pic 3
87 35689 60 13 Center Pic 3
88 35769 319 13 Center Pic 3
89 36108 60 13 Center Pic 3
90 36188 80 13 Center Pic 3
91 36288 339 13 Center Pic 3
92 36646 419 13 Center Pic 3
93 37085 60 13 Center Pic 3
94 37165 518 13 Center Pic 3
95 37703 139 13 Center Pic 3
96 38082 179 13 Center Pic 3
97 38281 159 13 Center Pic 3
98 38461 199 14 R2 Pic 3
99 38700 857 13 Center Pic 3
100 39836 817 13 Center Pic 3
101 40674 778 13 Center Pic 3
102 41471 40 13 Center Pic 3
103 41531 40 13 Center Pic 3
104 41591 159 13 Center Pic 3
105 41790 80 13 Center Pic 3
106 41890 817 0 Blank
107 42727 140 0 Blank
108 42887 219 13 Center Pic 3
109 43425 40 13 Center Pic 3
110 43525 179 12 L2 Pic 3
111 43724 80 12 L2 Pic 3
112 43824 40 12 L2 Pic 3
113 43924 40 13 Center Pic 3
114 43983 279 13 Center Pic 3
115 44282 60 13 Center Pic 3
116 44382 239 11 L1 Pic 3
117 44641 120 11 L1 Pic 3
118 44821 279 13 Center Pic 3
119 45120 339 13 Center Pic 3
120 45479 159 13 Center Pic 3
121 45658 239 13 Center Pic 3
122 45917 199 13 Center Pic 3
---------------------------------------------------
大家好,还要请各位帮忙:(为了清楚,写的有点罗嗦,抱歉)
数据简介:好麻烦的数据,但没办法。这是一组从机器中出来的数据(的一部分),ID指名每一行,ttotal是累计时间(ms),也就是机器运行的时间(不断累加直到这个项目停止),duration是相对应的后面的R2,R1,Center,L2,L1的时间(也就是在这几个部分上分别停留的时间,因为中间可能会有个别数据没被记录到,所以其总和不一定等于ttotal)。Pid是对应于area,也就是说每个图片分成的五个部分(L2,L1,Center,R1,R2),pid用数字表示这五个部分,(每个图片用5个数字对应于每个图片的五个部分)。area是图片被分成的五个部分(L1,L2,CNETER,R2,R1);最后一列是pic,这里共有三个pic (pic1-pic3).数据特点:每个图片分成两个段,每个图片的第一段是ttotal的前10000ms,第二段不定,但Marker结束则结束,然后pic变成另外一个图片,依此类推,第二个图片的前10000ms段, 随机段直到Marker结束,第三个图片。。,其中的空格或Marker不计。
每个又图片被分割成五个部分(area): L1,L2,Center, R2,R1 (pic本身),这一列中的content和Blank忽略不计,是机器自动生成的。结果: 每个图片每一秒(1000ms)五个部分(L1,L2,CENTER,R2,R1)的个数,及其对应的duration的总和及平均数(用总和除以相应的个数)。比如:对第一个图片,第一秒area里对应的L2,L1,CENTER,R1,R2的个数,然后求其对应的duration的总和,平均数(用总和除以相应的个数),第二秒area里对应的L1,L2,CENTER,R2,R1的个数。然后第二个图片里area里对应的L1,L2,CENTER,R2,R1的个数,然后再求其对应的duration的总和,平均数(用总和除以相应的个数)。直到结束。结果形式最好如下:
L1_cou L2_cou Center_cou R2_cou R1_cou L1_sum L2_sum Cen_sum R2_sum R1_sum L1_ave L2_ave Cen_ave R2_ave R1_ave
Pic1_1s
Pic1_2s
Pic1_3s
.....
Pic2_1s
pic2_2s
pic2_3s
pic2_4s
.....
pic3_1s
pic3_2s
pic3_3s
......其实后面还有好多图片:(
急多谢多谢!!
(前面的帖子自己改不回来,作废,呵呵)
1 878 219 0 Content er
2 1117 299 3 Center Pic 1
3 1437 159 3 Center Pic 1
4 1616 319 3 Center Pic 1
5 2094 40 3 Center Pic 1
6 2154 40 3 Center Pic 1
7 2254 279 3 Center Pic 1
8 2553 199 2 L2 Pic 1
9 2792 459 3 Center Pic 1
10 3271 618 3 Center Pic 1
11 3909 558 3 Center Pic 1
12 4706 299 3 Center Pic 1
13 5025 1176 3 Center Pic 1
14 6261 279 4 R2 Pic 1
15 6720 120 1 L1 Pic 1
16 6860 100 1 L1 Pic 1
17 6999 259 3 Center Pic 1
18 7278 259 3 Center Pic 1
19 7557 299 3 Center Pic 1
20 8116 60 3 Center Pic 1
21 8195 1077 3 Center Pic 1
22 9292 798 3 Center Pic 1
23 10109 279 3 Center Pic 1
24 10688 399 3 Center Pic 1
25 11106 159 0 Blank
26 11286 80 0 Blank
27 11385 718 0 Blank
28 12123 60 3 Center Pic 1
29 12203 458 3 Center Pic 1
30 12701 60 3 Center Pic 1
31 12801 179 4 R2 Pic 1
32 13020 498 3 Center Pic 1
33 13539 179 3 Center Pic 1
34 13778 40 3 Center Pic 1
35 14097 439 3 Center Pic 1
36 14556 797 3 Center Pic 1
37 15373 40 3 Center Pic 1
38 15473 279 0 Blank
39 15772 518 0 Content er
40 16469 40 8 Center Pic 2
41 16569 100 8 Center Pic 2
42 16689 1216 8 Center Pic 2
43 17965 179 9 R2 Pic 2
44 18164 279 8 Center Pic 2
45 18463 179 8 Center Pic 2
46 18663 518 8 Center Pic 2
47 19420 558 8 Center Pic 2
48 20258 518 8 Center Pic 2
49 20796 179 8 Center Pic 2
50 20995 1136 8 Center Pic 2
51 22192 100 7 L2 Pic 2
52 22331 738 8 Center Pic 2
53 23089 279 8 Center Pic 2
54 23388 478 8 Center Pic 2
55 23886 678 8 Center Pic 2
56 24803 837 8 Center Pic 2
57 25681 538 8 Center Pic 2
58 26239 160 8 Center Pic 2
59 26418 100 0 Blank
60 26538 219 0 Blank
61 26917 80 0 Blank
62 27017 279 0 Blank
63 27316 100 0 Blank
64 27435 299 8 Center Pic 2
65 27754 199 8 Center Pic 2
66 27974 259 8 Center Pic 2
67 28472 239 8 Center Pic 2
68 28731 80 8 Center Pic 2
69 29130 40 9 R2 Pic 2
70 29190 379 10 R1 Pic 2
71 29588 299 9 R2 Pic 2
72 29927 199 8 Center Pic 2
73 30147 40 8 Center Pic 2
74 30207 319 8 Center Pic 2
75 30545 159 8 Center Pic 2
76 30725 160 8 Center Pic 2
77 30904 120 0 Blank
78 31044 40 0 Blank
79 31104 219 0 Content er
80 31343 279 0 Content er
81 31961 80 13 Center Pic 3
82 32081 159 13 Center Pic 3
83 32260 678 13 Center Pic 3
84 32958 478 13 Center Pic 3
85 33456 399 13 Center Pic 3
86 34074 1336 13 Center Pic 3
87 35689 60 13 Center Pic 3
88 35769 319 13 Center Pic 3
89 36108 60 13 Center Pic 3
90 36188 80 13 Center Pic 3
91 36288 339 13 Center Pic 3
92 36646 419 13 Center Pic 3
93 37085 60 13 Center Pic 3
94 37165 518 13 Center Pic 3
95 37703 139 13 Center Pic 3
96 38082 179 13 Center Pic 3
97 38281 159 13 Center Pic 3
98 38461 199 14 R2 Pic 3
99 38700 857 13 Center Pic 3
100 39836 817 13 Center Pic 3
101 40674 778 13 Center Pic 3
102 41471 40 13 Center Pic 3
103 41531 40 13 Center Pic 3
104 41591 159 13 Center Pic 3
105 41790 80 13 Center Pic 3
106 41890 817 0 Blank
107 42727 140 0 Blank
108 42887 219 13 Center Pic 3
109 43425 40 13 Center Pic 3
110 43525 179 12 L2 Pic 3
111 43724 80 12 L2 Pic 3
112 43824 40 12 L2 Pic 3
113 43924 40 13 Center Pic 3
114 43983 279 13 Center Pic 3
115 44282 60 13 Center Pic 3
116 44382 239 11 L1 Pic 3
117 44641 120 11 L1 Pic 3
118 44821 279 13 Center Pic 3
119 45120 339 13 Center Pic 3
120 45479 159 13 Center Pic 3
121 45658 239 13 Center Pic 3
122 45917 199 13 Center Pic 3
---------------------------------------------------
大家好,还要请各位帮忙:(为了清楚,写的有点罗嗦,抱歉)
数据简介:好麻烦的数据,但没办法。这是一组从机器中出来的数据(的一部分),ID指名每一行,ttotal是累计时间(ms),也就是机器运行的时间(不断累加直到这个项目停止),duration是相对应的后面的R2,R1,Center,L2,L1的时间(也就是在这几个部分上分别停留的时间,因为中间可能会有个别数据没被记录到,所以其总和不一定等于ttotal)。Pid是对应于area,也就是说每个图片分成的五个部分(L2,L1,Center,R1,R2),pid用数字表示这五个部分,(每个图片用5个数字对应于每个图片的五个部分)。area是图片被分成的五个部分(L1,L2,CNETER,R2,R1);最后一列是pic,这里共有三个pic (pic1-pic3).数据特点:每个图片分成两个段,每个图片的第一段是ttotal的前10000ms,第二段不定,但Marker结束则结束,然后pic变成另外一个图片,依此类推,第二个图片的前10000ms段, 随机段直到Marker结束,第三个图片。。,其中的空格或Marker不计。
每个又图片被分割成五个部分(area): L1,L2,Center, R2,R1 (pic本身),这一列中的content和Blank忽略不计,是机器自动生成的。结果: 每个图片每一秒(1000ms)五个部分(L1,L2,CENTER,R2,R1)的个数,及其对应的duration的总和及平均数(用总和除以相应的个数)。比如:对第一个图片,第一秒area里对应的L2,L1,CENTER,R1,R2的个数,然后求其对应的duration的总和,平均数(用总和除以相应的个数),第二秒area里对应的L1,L2,CENTER,R2,R1的个数。然后第二个图片里area里对应的L1,L2,CENTER,R2,R1的个数,然后再求其对应的duration的总和,平均数(用总和除以相应的个数)。直到结束。结果形式最好如下:
L1_cou L2_cou Center_cou R2_cou R1_cou L1_sum L2_sum Cen_sum R2_sum R1_sum L1_ave L2_ave Cen_ave R2_ave R1_ave
Pic1_1s
Pic1_2s
Pic1_3s
.....
Pic2_1s
pic2_2s
pic2_3s
pic2_4s
.....
pic3_1s
pic3_2s
pic3_3s
......其实后面还有好多图片:(
急多谢多谢!!
(前面的帖子自己改不回来,作废,呵呵)
/*
标题:普通行列转换(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 课程
set @sql = '[' + @sql + ']'
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 姓名--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 + ' , cast(avg(分数*1.0) as decimal(18,2)) 平均分 , sum(分数) 总分 from tb group by 姓名'
exec(@sql) --SQL SERVER 2005 静态SQL。
select m.* , n.平均分 , n.总分 from
(select * from (select * from tb) a pivot (max(分数) for 课程 in (语文,数学,物理)) b) m,
(select 姓名 , cast(avg(分数*1.0) as decimal(18,2)) 平均分 , sum(分数) 总分 from tb group by 姓名) n
where m.姓名 = n.姓名--SQL SERVER 2005 动态SQL。
declare @sql varchar(8000)
select @sql = isnull(@sql + ',' , '') + 课程 from tb group by 课程
exec ('select m.* , n.平均分 , n.总分 from
(select * from (select * from tb) a pivot (max(分数) for 课程 in (' + @sql + ')) b) m ,
(select 姓名 , cast(avg(分数*1.0) as decimal(18,2)) 平均分 , sum(分数) 总分 from tb group by 姓名) n
where m.姓名 = n.姓名')drop table tb ------------------
------------------/*
问题:如果上述两表互相换一下:即表结构和数据为:
姓名 语文 数学 物理
张三 74 83 93
李四 74 84 94
想变成(得到如下结果):
姓名 课程 分数
---- ---- ----
李四 语文 74
李四 数学 84
李四 物理 94
张三 语文 74
张三 数学 83
张三 物理 93
--------------
*/create table tb(姓名 varchar(10) , 语文 int , 数学 int , 物理 int)
insert into tb values('张三',74,83,93)
insert into tb values('李四',74,84,94)
go--SQL SERVER 2000 静态SQL。
select * from
(
select 姓名 , 课程 = '语文' , 分数 = 语文 from tb
union all
select 姓名 , 课程 = '数学' , 分数 = 数学 from tb
union all
select 姓名 , 课程 = '物理' , 分数 = 物理 from tb
) t
order by 姓名 , case 课程 when '语文' then 1 when '数学' then 2 when '物理' then 3 end--SQL SERVER 2000 动态SQL。
--调用系统表动态生态。
declare @sql varchar(8000)
select @sql = isnull(@sql + ' union all ' , '' ) + ' select 姓名 , [课程] = ' + quotename(Name , '''') + ' , [分数] = ' + quotename(Name) + ' from tb'
from syscolumns
where name! = N'姓名' and ID = object_id('tb') --表名tb,不包含列名为姓名的其它列
order by colid asc
exec(@sql + ' order by 姓名 ')--SQL SERVER 2005 动态SQL。
select 姓名 , 课程 , 分数 from tb unpivot (分数 for 课程 in([语文] , [数学] , [物理])) t--SQL SERVER 2005 动态SQL,同SQL SERVER 2000 动态SQL。--------------------
/*
问题:在上述的结果上加个平均分,总分,得到如下结果:
姓名 课程 分数
---- ------ ------
李四 语文 74.00
李四 数学 84.00
李四 物理 94.00
李四 平均分 84.00
李四 总分 252.00
张三 语文 74.00
张三 数学 83.00
张三 物理 93.00
张三 平均分 83.33
张三 总分 250.00
------------------
*/select * from
(
select 姓名 as 姓名 , 课程 = '语文' , 分数 = 语文 from tb
union all
select 姓名 as 姓名 , 课程 = '数学' , 分数 = 数学 from tb
union all
select 姓名 as 姓名 , 课程 = '物理' , 分数 = 物理 from tb
union all
select 姓名 as 姓名 , 课程 = '平均分' , 分数 = cast((语文 + 数学 + 物理)*1.0/3 as decimal(18,2)) from tb
union all
select 姓名 as 姓名 , 课程 = '总分' , 分数 = 语文 + 数学 + 物理 from tb
) t
order by 姓名 , case 课程 when '语文' then 1 when '数学' then 2 when '物理' then 3 when '平均分' then 4 when '总分' then 5 enddrop table tb