1. 程式人生 > >SQL學習——聚合函式

SQL學習——聚合函式

Aggregate functions 聚合函式

從一組值計算單個結果集。

一.理論

1.COUNT

統計個數

SELECT COUNT(*)
FROM fake_apps
WHERE price=0;

2.SUM

SELECT SUM(downloads)
FROM fake_apps;

3.Max / Min

SELECT MAX(downloads)
FROM fake_apps;

SELECT MAX(price)
FROM fake_apps;

4.AVERAGE (AVG)

SELECT AVG(downloads)
FROM fake_apps;

SELECT AVG(price)
FROM fake_apps;

5.ROUND

取整

數字表示保留幾位小數

SELECT ROUND(price, 0)
FROM fake_apps;

SELECT ROUND(AVG(PRICE),2)
FROM fake_apps;

6.GROUP BY

GROUP BY 總在 WHERE之後 ,ORDER BY 和 LIMIT 之前

SELECT AVG(imdb_rating)
FROM movies
WHERE year = 1999;

SELECT AVG(imdb_rating)
FROM movies
WHERE year = 2000;

SELECT AVG(imdb_rating)
FROM movies
WHERE year = 2001;

!可以寫成
SELECT year,
   AVG(imdb_rating)
FROM movies
GROUP BY year
ORDER BY year;
!將每種類別的下載量計算
SELECT  category,SUM(downloads)
FROM fake_apps
GROUP BY category;

GROUP BY 使用數字表示按選中的第幾列排布

SELECT category, 
   price,
   AVG(downloads)
FROM fake_apps
GROUP BY 1;

7.HAVING

與WHERE很像但是排序比較靠後

WHERE過濾行 HAVING過濾組

SELECT price, 
   ROUND(AVG(downloads)),
   COUNT(*) AS number
FROM fake_apps
GROUP BY price
HAVING number>10;

二.實戰

將需要的顯示的列寫在select裡

 

函式strftime(__, timestamp)

%Y 返回年份(YYYY)

%m 返回月份(01-12)

%d 返回當月的日期(1-31)

%H 返回24小時制(00-23)

%M 返回分鐘(00-59)

%S 返回秒數(00-59)

SELECT strftime('%H', timestamp) AS 'Hour', 
   ROUND(AVG(score), 1) AS 'Average Score', 
   COUNT(*) AS 'Number of Stories'
FROM hacker_news
WHERE timestamp IS NOT NULL
GROUP BY 1
ORDER BY 2 DESC;

 

Database Schema

payments    2000 rows
id INTEGER
user_id INTEGER
amount REAL
status TEXT
pay_date TEXT
users   1000 rows
id INTEGER
first_name TEXT
last_name TEXT
email TEXT
password TEXT
watch_history    2000 rows
id INTEGER
user_id INTEGER
watch_date TEXT
watch_duration_in_minutes REAL

1.挑選最受歡迎的first_name 

SELECT first_name,COUNT(*) AS 'count'
FROM users
GROUP BY first_name
ORDER BY 2 DESC;

統計時間和次數

SELECT ROUND(watch_duration_in_minutes,0) AS 'duration',COUNT(*) AS 'count'
FROM watch_history
GROUP BY 1
ORDER BY 1 ASC;

 

3.統計成功消費客戶

SELECT user_id,SUM(amount)
FROM payments
WHERE status='paid'
GROUP BY 1 
ORDER BY 2 DESC;