惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - jierry

ASP.NET2.0控件一览---标准控件(2) ASP.NET2.0控件一览---标准控件(1) 控件开发时两种JS嵌入资源方式的使用 - jierry - 博客园 T-SQL tips(1)临时表和表变量 Flash Control for ASP.NET 2.0-Include Flash movies in your aspx pages 为DataGrid创建自定义列控件(四) 为DataGrid创建自定义列控件(三) - jierry - 博客园 为DataGrid创建自定义列控件(二) 为DataGrid创建自定义列控件(一) (转)SQLServer和Oracle的常用函数对比 《Effective C#》读书笔记(4) 《Effective C#》读书笔记(3) 《Effective C#》读书笔记(2) 《Effective C#》读书笔记(1) 选择合适的数据控件 自带图层的链接控件(DKLinks 1.0.0.323 ) 关于CodeBuild V3.0的一些想法 小工具:SQL存储过程解密修改工具 现在提供第一版的存储过程生成器下载,欢迎大家试用
交叉表应用-成绩统计
jierry · 2005-03-24 · via 博客园 - jierry

最近由于开发需要,涉及到了成绩统计这块,这里面设计到了sql交叉表的应用,试了试,觉得很有感想,现在写出来大家看看,有什么不对的欢迎指正^_^

首先我们建立如下的表:

sc(成绩表)
stuid clsid                                              scroe                                                
----- -------- -----------------------------------------------------
0101  1                                                  75.0
0102  1                                                  70.0
0103  1                                                  90.0
0101  2                                                  89.0
0102  2                                                  80.0
0103  2                                                  99.0
0101  3                                                  89.0
0102  3                                                  79.0
0103  3                                                  67.0

其中stuid表示学生编号,clsid表示课程编号,scroe表示成绩

stu(学生表)
stuid       stuname                                           
----------- --------------------------------------------------
101         张三
102         李四
103         王五

cls(课程表)
clsid       name                                              
----------- --------------------------------------------------
1           语文
2           数学
3           英语

接下来就是关键了,其实也不算好复杂,就是用到了动态sql

declare @sql nvarchar(4000),@sql1 nvarchar(4000)
select @sql='',@sql1=''

select @sql=@sql+',['+name+']=sum(case clsid when '''+clsid+''' then scroe else 0 end)',
       @sql1=@sql1+',['+name+'名次]=(select sum(1) from # where ['+name+']>=a.['+name+'])'      
from(select distinct b.clsid,c.name from sc as b inner join cls as c on c.clsid=b.clsid) as a order by clsid

exec('select stuid 学号'+@sql+',总成绩=sum(scroe)
,平均分=Convert(dec(5,1),avg(scroe)),总名次=(select sum(1) from(select stuid,aa=sum(scroe) from sc group by stuid) aa where sum(a.scroe)<=aa) into # from sc as a group by stuid select b.stuname as 姓名,a.*'+@sql1+' from # as a inner join stu as b on a.学号=b.stuid')

以下就是结果:

姓名    学号    语文    数学    英语    总成绩  平均分 总名次 语文名次 数学名次 英语名次   
---------------------------------------------------- --------------------------------------
张三     0101    75.0    89.0      89.0     253.0      84.3         2             2                2               1
李四     0102    70.0    80.0      79.0     229.0      76.3         3             3                3               2
王五     0103    90.0    99.0      67.0     256.0      85.3         1             1                1               3

这就是交叉表的用法,对于报表统计很有用处,我们可以依此类推写出功能更强大的查询。