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

推荐订阅源

T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
AWS News Blog
AWS News Blog
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
美团技术团队
T
Threatpost
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
Blog — PlanetScale
Blog — PlanetScale
C
Cybersecurity and Infrastructure Security Agency CISA
F
Full Disclosure
博客园_首页
N
Netflix TechBlog - Medium
Security Latest
Security Latest
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
博客园 - Franky
P
Palo Alto Networks Blog
Project Zero
Project Zero
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
GbyAI
GbyAI

博客园 - X龙

远程唤醒wol(Wake On Lan) 我的附件列表 github项目管理 js提交验证 web模拟慢网速环境 按钮提交事件处理(以下方法兼容ie,firefox,chrome) js 格式化日期 div+css搭建系统页面框架 Win7下如何在任务栏显示操作中心的图标 清除右键图形属性 图形选项 .net软件工程师面试知识点 设置JqueryUI DatePicker默认语言为中文 asp.net mvc2升级到asp.net mvc3 解决32位ie不能上网,64位ie能上网 seo robots.txt编写 xml xmlnamespace sql server 2005生成insert语句,同时完成多表(Bug:插入语句列列表最后一列列名后还带有,) input type=file显示及清空值 预防SQL注入攻击
pivot与unpivot用法
X龙 · 2012-04-17 · via 博客园 - X龙

Posted on 2012-04-17 21:51  X龙  阅读(512)  评论()    收藏  举报

/*
productid   Name Age Email            Mobile
----------- ----------------------- -----------
1           long 88  test@gmail.com 13000000000
2           lz 
 
pivot执行步骤:将行转列,分组确定行数,添加in后面的列,列值是应用聚和函数后的值
1. 隐式分组(对左输入中的所有列除FieldValue,FieldID分组,得到所有行)
2. 隔离值(为IN子句中的每个列使用case when FieldID=<目标列> then FieldValue end
3. 应用聚和函数(为2中的case子句应用聚和函数,此处为min)
*/
select productid,min([1]) [Name],min([2]) Age,min([3]) Email,min([4]) Mobile 
from prodfieldvalue
    pivot (min(fieldvalue) for FieldID in([1],[2],[3],[4])) as d
group by productid
 
/*
结果:
productid   FieldValue            FiledID
----------- ------------------- ----------
1           long                1
1           88                    2
1           test@gmail.com.com  3
1           1300000000            4
2           lz                    1
 
unpivot:执行步骤:将列转行,复制unpivot左输入中的行,左输入每一行复制IN中值的数量的行数,
添加新列,列名为for后面的值,按in中值的顺序为此列的每一行赋值,过滤掉新列值为Null的行。
1. 生成副本
2. 隔离目标列值
3. 过滤掉带有NULL值的行
*/
select * from (
select productid,min([1]) [1],min([2]) [2],min([3]) [3],min([4]) [4]
from prodfieldvalue
    pivot (min(fieldvalue) for FieldID in([1],[2],[3],[4])) as d
group by productid ) t
    unpivot (FieldValue for FiledID in ([1],[2],[3],[4])) as tt