[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;

时间: 2024-08-03 10:41:15

[Postgres] Group and Aggregate Data in Postgres的相关文章

MongoDB聚合运算之group和aggregate聚集框架简单聚合(10)

聚合运算之group 语法: db.collection.group( { key:{key1:1,key2:1}, cond:{}, reduce: function(curr,result) { }, initial:{}, finalize:function() { } } ) key: 分组字段 cond:查询条件 reduce:聚合函数 initial:初始化 finalize:统计一组后的回调函数 #查询每个栏目下的商品数量 db.goods.group( { key:{cat_id

hive+postgres安装部署过程

master节点安装元数据库,采用postgres:#useradd postgres#password postgressu - postgreswget https://ftp.postgresql.org/pub/source/v10beta2/postgresql-10beta2.tar.gztar zxvf postgresql-10beta2.tar.gzcd postgresql-10beta2 ./configuremakesumake install mkdir /usr/lo

PostgreSQL源码安装文档

This document describes the installation of PostgreSQL using the source    code distribution. (If you are installing a pre-packaged distribution,    such as an RPM or Debian package, ignore this document and read the    packager's instructions instea

postgresql 使用指南

centos系列安装分为: yum安装 源码安装 一.yum安装 按照官方的安装文档进行. 安装postgresql官方yum仓库 yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm 安装postgresql数据库 yum install postgresql96-server postgresql96-contrib 初

Postgresql学习笔记

Postgresql源码安装: 从http://www.postgresql.org/download/下载源码v9.4.5编译安装 ./configuremakesumake install  //安装完成adduser postgresmkdir /usr/local/pgsql/datachown postgres /usr/local/pgsql/datasu - postgres/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data  

用Pgpool-II实现Postgresql高可用集群

其实整个安装和配置过程比较简单,官方网站有比较好的文档,在此只是根据前几天的实际部署整理一下.(实际执行的命令都用红色标出) 服务器: 10.18.27.181    pgpool服务器 --------此服务器上需要安装pgpool和pg 10.18.27.183    data node 1  --------此服务器上安装pg 10.18.27.184    data node 2  --------此服务器上安装pg 10.18.27.185    data node 3  ------

postgresql - 三种安装方式

最近接触了postgresql的安装,和大家分享一下. 一.简 介 PostgreSQL 是一种非常复杂的对象-关系型数据库管理系统(ORDBMS),也是目前功能最强大,特性最丰富和最复杂的自由软件数据库系统.有些特性甚至连商业数据库都不具备.这个起源于伯克利(BSD)的数据库研究计划目前已经衍生成一项国际开发项目,并且有非常广泛的用户. 优势:http://www.cnblogs.com/zhangpengme/archive/2011/12/01/2271092.html 官网:http:/

pgAdminIII使用图解

原文地址:http://www.2cto.com/database/201312/267218.html pgAdmin III简介 要打开一个到服务的连接,在树中选择所需的服务,并双击它,或使用“工具”菜单上的连接即可. 一.主窗体 在主窗口中,显示数据库的结构.您可以创建新的对象,删除和编辑现有的对象,如果你使用的是当前连接到数据库用户的权限,在主窗口的左侧显示所有服务器,以及它们所包含的对象树.右上方显示,目前在树中选定的对象的详细信息.右下方包含一个SQL脚本.二.导航菜单功能1.文件菜

搭建双节点pg_pool+主从postgresql架构

复制方式           优点                                                                 缺点 ------------------------------------------------------------------------------------同步 数据一致性高                                            1.写入性能低