SQL Serise Part III (SQL Aggregation)

COUNT

# count the number of rows in a table
SELECT COUNT(*) FROM table;

SUM, MIN, MAX, AVG

# find total amount of col in the table
SELECT SUM(col) AS cl # you can also use some (* + /) operators here
FROM table;

# find the minimal value of col in table, you can also use ORDER BY to implement it
SELECT MIN(col) AS cl
FROM table;

# find the maximal value of col in table, you can also use ORDER BY DESC to implement it
SELECT MAX(col) AS cl
FROM table;

# calculate the average value of col in table
SELECT AVG(col) AS cl
FROM table;

GROUP BY

# The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
SELECT COUNT(id), name
FROM table
GROUP BY name; # will show the amount for each name

DISTINCT

# Use DISTINCT to test if there are any accounts associated with more than one region.
SELECT DISTINCT id, name
FROM table;

HAVING

# The HAVING clause was used with aggregate functions instead of WHERE
SELECT COUNT(id), name
FROM table
GROUP BY name
HAVING COUNT(id) > n;

DATE Functions

# DATE_TRUNC : Truncate to specified precision
# date_trunc('hour', timestamp '2001-02-16 20:38:40') => 2001-02-16 20:00:00
SELECT DATE_TRUNC('month', table1.time_col) cl1, SUM(table1.spent_col) cl2
FROM table1
JOIN table2
ON table1.id = table2.foreign_key_id
WHERE table2.name = 'someting'
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1;

# DATE_PART : Get subfield
# date_part('hour', timestamp '2001-02-16 20:38:40') => 20
SELECT DATE_PART('year', time_col) cl1,  SUM(total_col) cl2
FROM table
GROUP BY 1
ORDER BY 2 DESC;

CASE Statements

# Evaluate conditions and return a value when the first condition is met
# something will be wrong when the total_qty equal 0:
SELECT id, aid, total_price/total_qty AS unit_price
FROM table
LIMIT 10;
# so use CASE statement can avoid the error:
SELECT id, aid, CASE WHEN total_qty = 0 OR total_qty IS NULL THEN 0
                 ELSE total_price/total_qty 
            END AS unit_price
FROM table
LIMIT 10;

# CASE with aggregations
SELECT table1.name, SUM(total_price) total_spent, 
     CASE WHEN SUM(total_price) > 200000 THEN 'top'
          WHEN  SUM(total_price) > 100000 THEN 'middle'
          ELSE 'low' 
     END AS cl
FROM table2
JOIN table1
ON table2.aid = table1.id 
GROUP BY table1.name
ORDER BY 2 DESC;
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,504评论 0 13
  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,254评论 0 7
  • 我们都曾经希望别人给我们安全感、给我们依靠,我们兜兜转转的寻找,幸运儿找到了那个为你保驾护航的人,无论你做什么,他...
    皓月月月阅读 219评论 0 0
  • 对于单纯 我的理解就是 想把球 投进对面的另一只手套 是对着天空举手 是长矛的长 锤子的硬 是把书翻开 把盖子阖上...
    草茅阅读 717评论 3 13
  • 自2009年分裂而生的阿密特小姐石破天惊过后,本尊张惠妹就顺理成章为自己的音乐属性分了工,暴烈侵略的阿密特单刀直入...
    溪仔ShineY阅读 244评论 0 0