博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Postgres] Group and Aggregate Data in Postgres
阅读量:5341 次
发布时间:2019-06-15

本文共 1217 字,大约阅读时间需要 4 分钟。

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,titleFROM moviesLIMIT 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 genresGROUP BY genre;

 

转载于:https://www.cnblogs.com/Answer1215/p/6532739.html

你可能感兴趣的文章
建造者模式
查看>>
ArraySort--冒泡排序、选择排序、插入排序工具类demo
查看>>
composer 安装laravel
查看>>
8-EasyNetQ之Send & Receive
查看>>
Android反编译教程
查看>>
java重写LinkedList
查看>>
zTree节点重叠或者遮挡
查看>>
List<string> 去重复 并且出现次数最多的排前面
查看>>
js日志管理-log4javascript学习小结
查看>>
Android之布局androidmanifest.xml 资源清单 概述
查看>>
How to Find Research Problems
查看>>
Linux用户管理
查看>>
数据库第1,2,3范式学习
查看>>
《Linux内核设计与实现》第四章学习笔记
查看>>
使用iperf测试网络性能
查看>>
struts2入门之准备工作
查看>>
从C语言的弱类型属性说起
查看>>
图片的显示隐藏(两张图片,默认的时候显示第一张,点击的时候显示另一张)...
查看>>
Docker 安装MySQL5.7(三)
查看>>
python 模块 来了 (调包侠 修炼手册一)
查看>>