[Postgres] Group and Aggregate Data in Postgres
How can we see a histogram of movies on IMDB with a particular rating? Or how much movies grossed at the box office each month? Or how many movies there are of each genre? These are examples of data aggregation questions, and this lesson will teach us how to answer them.

In the table we have 'action', 'animation'... 'short' categories. They all use 'true' of 'false'.
What if we want to use those by its categoreis not just ture of false?
We can use 'CASE' in Postgres.
SELECT
CASE
WHEN action=true THEN 'action'
WHEN animation=true THEN 'animation'
WHEN comedy=true THEN 'comedy'
WHEN drama=true THEN 'drama'
WHEN short=true THEN 'short'
ELSE 'other'
END AS genre,
title
FROM movies
LIMIT 100

And now we want to get "how many movies for each category" from previous result.
What we can do is using "GROUP BY" and "WITH":
WITH genres AS(
SELECT
CASE
WHEN action=true THEN 'action'
WHEN animation=true THEN 'animation'
WHEN comedy=true THEN 'comedy'
WHEN drama=true THEN 'drama'
WHEN short=true THEN 'short'
ELSE 'other'
END AS genre,
title
FROM movies
LIMIT 100
)
SELECT genre,
COUNT(*)
FROM genres
GROUP BY genre;

[Postgres] Group and Aggregate Data in Postgres的更多相关文章
- Hierarchical data in postgres
https://coderwall.com/p/whf3-a/hierarchical-data-in-postgres --------------------------------------- ...
- 云原生 PostgreSQL 集群 - PGO:来自 Crunchy Data 的 Postgres Operator
使用 PGO 在 Kubernetes 上运行 Cloud Native PostgreSQL:来自 Crunchy Data 的 Postgres Operator! Cloud Native Po ...
- MongoDB聚合运算之group和aggregate聚集框架简单聚合(10)
聚合运算之group 语法: db.collection.group( { key:{key1:1,key2:1}, cond:{}, reduce: function(curr,result) { ...
- 【mongoDB高级篇①】聚集运算之group与aggregate
group 语法 db.collection.group({ key:{field:1},//按什么字段进行分组 initial:{count:0},//进行分组前变量初始化,该处声明的变量可以在 ...
- 2.4 The Object Model -- Computed Properties and Aggregate Data with @each(计算的属性和使用@each聚合数据)
1. 通常,你可能有一个计算的属性依赖于数组中的所有元素来确定它的值.例如,你可能想要计算controller中所有todo items的数量,以此来确定完成了多少任务. export default ...
- [Postgres] Update and Delete records in Postgres
Delete example: DELETE FROM movies ; Update example: UPDATE movies ;
- Linux上安装postgres 10.5
由于接触了华为的elk大数据平台,里面封装的是postgres ,就想着安装一下,熟悉一下postgres数据. 安装包下载:https://www.postgresql.org/ftp/source ...
- hive+postgres安装部署过程
master节点安装元数据库,采用postgres:#useradd postgres#password postgressu - postgreswget https://ftp.postgresq ...
- PostgreSQL源码安装文档
This document describes the installation of PostgreSQL using the source code distribution. (If yo ...
随机推荐
- 【2017"百度之星"程序设计大赛 - 初赛(B)】小小粉丝度度熊
[链接]http://acm.hdu.edu.cn/showproblem.php?pid=6119 [题意] 在这里写题意 [题解] 先把相交的部分合成一个区间. 这个可以用排序,加个简单的处理就能 ...
- Spring3拦截引发的问题——WEB开发中的client路径
什么是client路径? 第一类.也就是html或js文件等client訪问的文件里的路径,这里包含一些资源文件的引入(js.css还有各种图片等),或是跳转到静态html页面,总之获取的都是静态资源 ...
- Vue自定义组件
- Mongodb总结1-启动和Shell脚本
2013年,还在秒针,当时听说了Mongodb,就学习了下,搞了下HelloWorld.主要是熟悉Mongodb的启动.命令行的Shell脚本.Java访问的CRUD. 今天,由于需要,再次回顾和进一 ...
- P2P平台项目建设几点注意事项
种选择 a.资金池,接入第三方支付,财付通,宝付等,比如一起好 b.第三方资金托管,易宝支付.汇付天下等 c.银行存管,资金放在银行 资金池政策风险,不得不考虑. 每一种模式,技术实现都有变化,需要考 ...
- amazeui学习笔记--css(常用组件4)--关闭按钮Close
amazeui学习笔记--css(常用组件4)--关闭按钮Close 一.总结 1.关闭按钮基本用法:关闭按钮样式,可以结合其他不同组件使用.对 <a> 或者 <button> ...
- Java判断1个字符串中出现了几次其他字符串
public class Test { public static int count(String text,String sub){ , start =; ){ start += sub.leng ...
- 软件——关于C,typedef
1;;写博客能让我慢下来,仔细思考 1;;这篇博客大多摘自网上 1;; 不管实在C还是C++代码中,typedef这个词都不少见,当然出现频率较高的还是在C代码中.typedef与#define有些相 ...
- Linux常用命令及解析
基本日常命令 init 3 (进入命令行页面) steup (设置网络) exit (退出用户) pwd(查看当前所在目录) date(查看当前系统时间) 举例:(date +%Y-%m-%d)以年月 ...
- jmeter--函数助手对话框之参数详解
详解JMeter函数和变量 测试人员可以在JMeter的选项菜单中找到函数助手对话框("Function Helper"对话框),如图11-1所示. 图11-1 函数助手(Func ...