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

推荐订阅源

K
Kaspersky official blog
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
D
Docker
博客园 - 司徒正美
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
U
Unit 42
J
Java Code Geeks
A
About on SuperTechFans
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
H
Hacker News: Front Page
G
Google Developers Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
S
Secure Thoughts
GbyAI
GbyAI
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - MangaGo

在网页里让文本框只能输入数字的一种方法。外加回车换Tab - MangaGo - 博客园 (转) WINDOWS下的使用VS.NET2005的SVN手记 学习.Net的经典网站 Subversion快速入门教程 ASP.NET 2.0 下配置 FCKeditor - MangaGo asp中验证码图片为BMP格式,但是不显示 [Serializable]在C#中的作用-NET 中的对象序列化 VSS服务器安装配置 TimeSpan 用法 求离最近发表时间的函数 - MangaGo javascript判断密码安全级别 document.getElementById 你真正了解了吗 - MangaGo - 博客园 js获得当前日期并 显示 - MangaGo - 博客园 永远不要跟父母说的十句话,谁没说过? 在ASP.NET里轻松实现缩略图 用ASP.NET缓存提高站点性能【转载】 一个冒泡排序算法 一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现 阶乘的经典算法! 关于上传文件的大小限制
一个sql中常遇到的表结构转换问题
MangaGo · 2007-12-25 · via 博客园 - MangaGo

score表结构如

name  subject  score
-----------------------------------
大都    语文     59
大都    数学     78
大都    外语     89
大都    物理     98
小都    语文     90
小都    外语     80
小都    物理     70
德国    语文     90
德国    数学     50
德国    外语     80
德国    物理     89
哈哈    语文     99
哈哈    数学     80
哈哈    物理     89

现需转换成表结构如:

姓名 数学 外语 物理 语文
-----------------------------------------
大都   78    89     98     59
德国   50    80     89     90
哈哈   80    0       89     99
小都   0      80     70     90

解决之道:

--当出现的subject的个数不定的时候

declare @sql varchar(8000)
set @sql='SELECT name as 姓名'
select @sql=@sql+',['+subject+']=MAX(CASE subject WHEN '''+subject+''' THEN score ELSE 0 END)' from score group by subject
exec(@sql+' FROM score GROUP BY name')

--当出现的subject的个数确定的时候

select name as 姓名,
数学
=MAX(CASE WHEN subject='数学' THEN score ELSE 0 END),
外语
=MAX(CASE WHEN subject='外语' THEN score ELSE 0 END),
物理
=MAX(CASE WHEN subject='物理' THEN score ELSE 0 END),
语文
=MAX(CASE WHEN subject='语文' THEN score ELSE 0 END)
from score
group by name