How to Pivot Data in SQL?
1. Introduction
If you think "pivot data" is too terminology, we can also describe the same thing as interchange rows and columns data. Not necessaryly but normally, it will also have data aggregation during the process.
Different people may have different idea when "pivot data" appears to them:
If you are an Excel expert, you may think it as creating an excel pivot table and dragging data tab with mouse.
If you use R with tidyverse packages, you may think it as changing tidy data into un-tidy data. It is like making a long table wide or making a wide table long. This process can be done by pivot_wider() or pivot_longer().
If you use Python and pandas package, this can be done by pd.pivot_table() function. I think they also has a function called pd.melt() but I don't use it much.
I won't go to detail about above cases because in this article we will focus on SQL: How we can pivot data in SQL?
2. The problem
Suppose we have a table like below(we call it Products):
What we can do if we want it change to table like below(we call it Results):
Ideally speaking, the second kind of table should never exists, because it is not suitable for further analyse. But in some situation it is also useful when representing the result.
3. SQL process on pivot
3.1 use "group by" with conditional summarise.
A pivot table is nothing miracle. It is only a few independent "group by" summarise results join together form left to right.
We can repeat this process step by step by ourself.
select product_id, sum(price) as store1 from Products where store = 'store1' group by product_id
Now we will have:
We can repeat this process for each store one by one. This is what actually a pivot program or a pivot function from some library will do under the hood.
A "group by" summarise with "where" clause is so call "conditional summarise". Another way of writting "conditional summarise" equally is using "case when".
For example, the first query, we can rewrite it as:
select product_id, sum(case store when 'store1' then price else null end) as store1 from Products group by product_id
Now we will have a same result:
This method is a little bit more useful because it can help us avoid using "where" clause for each case independently. So that we can write them in one query.
select product_id, sum(case store when 'store1' then price end) as store1, sum(case store when 'store2' then price end) as store2, sum(case store when 'store3' then price end) as store3 from Products group by product_id
3.2 use pivot() function
There is function call pivot() in MS SQL Server, it can be used like below:
select * from Products pivot(sum(price) for store in (store1, store2, store3)) pt
We will have our expecting result a well:
This is a useful function but do not use it before you really understand what pivot is doing. Otherwise you will confuse youself easily. You can see how this short function is hidding big infomation to us.
3.3 the backward question
We may also meet some situations that we want to make a backward manipulation. That is, pivot the Result table to Products table.
It is a tricky one but after you know the process you will easy to re-use in future.
select product_id, "store1" as store, store1 as price from Results where store1 is not null union select product_id, "store2" as store, store2 as price from Results where store2 is not null union select product_id, "store3" as store, store3 as price from Results where store3 is not null
The "union" keyword can help us combine table together upside down. That is what we need in this situation.
原文地址:https://www.cnblogs.com/drvongoosewing/p/14885542.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 潘石屹用Python解决100个问题 | 斐波那契数列分数
- Leetcode No.7 整数反转
- 后端程序员必备的 Linux 基础知识+常见命令(近万字总结)
- XSS Game分析以及知识点总结
- Web 图形可视化 SQL 优化神奇,真香!
- C++ vector 容器浅析
- C++中的STL中map用法详解
- C++ pair(对组)的简单了解
- elasticSearch学习(六)
- centos7搭建LDAP服务器
- zookeeper is not a recognized option zookeeper参数不支持
- 快来看看你是不是“假的”DBA
- 基于docker搭建gitlab
- 理解Future及FutureTask的实现
- centos7 hbase1.4.13+hadoop2.7.1+单机环境搭建