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

推荐订阅源

V
Vulnerabilities – Threatpost
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Privacy International News Feed
F
Full Disclosure
P
Proofpoint News Feed
The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Blog of Author Tim Ferriss
T
Threatpost
L
Lohrmann on Cybersecurity
I
Intezer
S
SegmentFault 最新的问题
小众软件
小众软件
T
Threat Research - Cisco Blogs
MongoDB | Blog
MongoDB | Blog
美团技术团队
NISL@THU
NISL@THU
罗磊的独立博客
N
News | PayPal Newsroom
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - 聂微东
W
WeLiveSecurity
Microsoft Security Blog
Microsoft Security Blog
阮一峰的网络日志
阮一峰的网络日志
Scott Helme
Scott Helme
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
A
Arctic Wolf
P
Privacy & Cybersecurity Law Blog
Attack and Defense Labs
Attack and Defense Labs
I
InfoQ
Microsoft Azure Blog
Microsoft Azure Blog
S
Securelist
D
Darknet – Hacking Tools, Hacker News & Cyber Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Schneier on Security
Schneier on Security
Know Your Adversary
Know Your Adversary
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
U
Unit 42
The Cloudflare Blog
T
Tenable Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Recent Announcements
Recent Announcements
D
DataBreaches.Net
量子位

博客园 - 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