SQL学习笔记(五):汇总分类

135 阅读1分钟

1. Aggregate Functions (聚合函数)

image.png
  • ()里面也可以是表达式 🌰中sum(invoice_total*1.1) as total
  • count 函数统计的是not null 的,当一个schema可能有null值时,count出的数量会小于总行数
  • count(*)表示统计行数,这种就可以避免schema有null值,可以统计出实际的行数
  • count (distinct invoice_total) 表示count同时去掉了重复值

2. Group by 子句

image.png

Attention: group by 多个schema,不需要用到()

3. Having 子句

  • Having : 可用于筛选group by 之后的数据
  • Where : 用于group by 之前筛选数据
image.png

4. The Rollup Operator (Rollup运算符)

  • with rollup: 得到group by 下面的求和(类似excel里面的分类之后的汇总)
  • 在用with rollup的情况下,group by 只能用本身的列名(指的是 as 之前的名字),否则会报错error
image.png

🌰结果:第3行null就是with rollup的结果

image.png