AVG - 计算一组值的平均值
COUNT - 统计一组值的数量
MAX - 计算一组值的最大值
MIN - 计算一组值的最小值
SUM - 计算一组值的和值
STRING_AGG - 连接一组字符串
SELECT AVG(salary),
COUNT(*),
MAX(salary),
MIN(salary),
SUM(salary)
FROM employees
WHERE department_id = 60;
avg |count|max |min |sum |
---------------------|-----|-------|-------|--------|
5760.0000000000000000| 5|9000.00|4200.00|28800.00|
函数参数前添加DISTINCT可以在计算时排除重复值。
忽略参数中的 NULL 值。
SELECT COUNT(*),
COUNT(DISTINCT salary),
COUNT(commission_pct)
FROM employees
WHERE department_id = 60;
count|count|count|
-----|-----|-----|
5| 4| 0|
SELECT STRING_AGG(first_name, ';' ORDER BY salary DESC)
FROM employees
WHERE department_id = 60;
string_agg |
---------------------------------|
Alexander;Bruce;David;Valli;Diana|
SELECT department_id,
AVG(salary),
COUNT(*),
MAX(salary),
MIN(salary),
SUM(salary)
FROM employees
GROUP BY department_id
ORDER BY department_id;
department_id|avg |count|max |min |sum |
-------------|----------------------|-----|--------|--------|---------|
10| 4400.0000000000000000| 1| 4400.00| 4400.00| 4400.00|
20| 9500.0000000000000000| 2|13000.00| 6000.00| 19000.00|
30| 4150.0000000000000000| 6|11000.00| 2500.00| 24900.00|
40| 6500.0000000000000000| 1| 6500.00| 6500.00| 6500.00|
50| 3475.5555555555555556| 45| 8200.00| 2100.00|156400.00|
60| 5760.0000000000000000| 5| 9000.00| 4200.00| 28800.00|
70|10000.0000000000000000| 1|10000.00|10000.00| 10000.00|
80| 8955.8823529411764706| 34|14000.00| 6100.00|304500.00|
90| 19333.333333333333| 3|24000.00|17000.00| 58000.00|
100| 8601.3333333333333333| 6|12008.00| 6900.00| 51608.00|
110|10154.0000000000000000| 2|12008.00| 8300.00| 20308.00|
| 7000.0000000000000000| 1| 7000.00| 7000.00| 7000.00|
SELECT department_id
FROM employees
GROUP BY department_id
ORDER BY department_id;
department_id|
-------------|
10|
20|
30|
40|
50|
60|
70|
80|
90|
100|
110|
|
SELECT DISTINCT department_id
FROM employees
ORDER BY department_id;
SELECT department_id,
job_id,
AVG(salary),
COUNT(*),
MAX(salary),
MIN(salary),
SUM(salary)
FROM employees
GROUP BY department_id, job_id
ORDER BY department_id, job_id;
department_id|job_id |avg |count|max |min |sum |
-------------|----------|----------------------|-----|--------|--------|---------|
10|AD_ASST | 4400.0000000000000000| 1| 4400.00| 4400.00| 4400.00|
20|MK_MAN |13000.0000000000000000| 1|13000.00|13000.00| 13000.00|
20|MK_REP | 6000.0000000000000000| 1| 6000.00| 6000.00| 6000.00|
30|PU_CLERK | 2780.0000000000000000| 5| 3100.00| 2500.00| 13900.00|
30|PU_MAN |11000.0000000000000000| 1|11000.00|11000.00| 11000.00|
40|HR_REP | 6500.0000000000000000| 1| 6500.00| 6500.00| 6500.00|
50|SH_CLERK | 3215.0000000000000000| 20| 4200.00| 2500.00| 64300.00|
50|ST_CLERK | 2785.0000000000000000| 20| 3600.00| 2100.00| 55700.00|
50|ST_MAN | 7280.0000000000000000| 5| 8200.00| 5800.00| 36400.00|
60|IT_PROG | 5760.0000000000000000| 5| 9000.00| 4200.00| 28800.00|
70|PR_REP |10000.0000000000000000| 1|10000.00|10000.00| 10000.00|
80|SA_MAN | 12200.000000000000| 5|14000.00|10500.00| 61000.00|
80|SA_REP | 8396.5517241379310345| 29|11500.00| 6100.00|243500.00|
90|AD_PRES | 24000.000000000000| 1|24000.00|24000.00| 24000.00|
90|AD_VP | 17000.000000000000| 2|17000.00|17000.00| 34000.00|
100|FI_ACCOUNT| 7920.0000000000000000| 5| 9000.00| 6900.00| 39600.00|
100|FI_MGR |12008.0000000000000000| 1|12008.00|12008.00| 12008.00|
110|AC_ACCOUNT| 8300.0000000000000000| 1| 8300.00| 8300.00| 8300.00|
110|AC_MGR |12008.0000000000000000| 1|12008.00|12008.00| 12008.00|
|SA_REP | 7000.0000000000000000| 1| 7000.00| 7000.00| 7000.00|
SELECT department_id,
job_id,
AVG(salary),
COUNT(*),
MAX(salary),
MIN(salary),
SUM(salary)
FROM employees
GROUP BY department_id;
SQL 错误 [42803]: ERROR: column "employees.job_id" must appear in the GROUP BY clause or be used in an aggregate function
Position: 31
SELECT department_id,
AVG(salary),
COUNT(*),
MAX(salary),
MIN(salary),
SUM(salary)
FROM employees
WHERE AVG(salary) > 10000
GROUP BY department_id
ORDER BY department_id;
SQL 错误 [42803]: ERROR: aggregate functions are not allowed in WHERE
Position: 150
SELECT department_id,
AVG(salary),
COUNT(*),
MAX(salary),
MIN(salary),
SUM(salary)
FROM employees
GROUP BY department_id
HAVING AVG(salary) > 10000
ORDER BY department_id;
department_id|avg |count|max |min |sum |
-------------|----------------------|-----|--------|--------|--------|
90| 19333.333333333333| 3|24000.00|17000.00|58000.00|
110|10154.0000000000000000| 2|12008.00| 8300.00|20308.00|
SELECT department_id,
COUNT(*) AS headcount
FROM employees
WHERE salary > 10000
GROUP BY department_id
HAVING COUNT(*) > 2;
department_id|headcount|
-------------|---------|
80| 8|
90| 3|
CREATE TABLE sales (
item VARCHAR(10),
year VARCHAR(4),
quantity INT
);
INSERT INTO sales VALUES('apple', '2018', 800);
INSERT INTO sales VALUES('apple', '2018', 1000);
INSERT INTO sales VALUES('banana', '2018', 500);
INSERT INTO sales VALUES('banana', '2018', 600);
INSERT INTO sales VALUES('apple', '2019', 1200);
INSERT INTO sales VALUES('banana', '2019', 1800);
按照产品和年度的组合进行分组;
按照产品进行分组;
按照年度进行分组;
所有数据分为一组。
-- 按照产品和年度的组合进行分组
SELECT item, year, SUM(quantity)
FROM sales
GROUP BY item, year;
item |year|sum |
------|----|----|
banana|2019|1800|
apple |2019|1200|
apple |2018|1800|
banana|2018|1100|
-- 按照产品进行分组
SELECT item, NULL AS year, SUM(quantity)
FROM sales
GROUP BY item;
item |year|sum |
------|----|----|
banana| |2900|
apple | |3000|
-- 按照年度进行分组
SELECT NULL AS item, year, SUM(quantity)
FROM sales
GROUP BY year;
item|year|sum |
----|----|----|
|2018|2900|
|2019|3000|
-- 所有数据分为一组
SELECT NULL AS item, NULL AS year, SUM(quantity)
FROM sales;
item|year|sum |
----|----|----|
| |5900|
SELECT item, year, SUM(quantity)
FROM sales
GROUP BY GROUPING SETS (
(item, year),
(item),
(year),
()
);
item |year|sum |
------|----|----|
| |5900|
banana|2019|1800|
apple |2019|1200|
apple |2018|1800|
banana|2018|1100|
banana| |2900|
apple | |3000|
|2018|2900|
|2019|3000|
CUBE ( c1, c2, c3 )
GROUPING SETS (
( c1, c2, c3 ),
( c1, c2 ),
( c1, c3 ),
( c2, c3 ),
( c1 ),
( c2 ),
( c3 ),
( )
)
SELECT item, year, SUM(quantity)
FROM sales
GROUP BY CUBE (item,year);
item |year|sum |
------|----|----|
| |5900|
banana|2019|1800|
apple |2019|1200|
apple |2018|1800|
banana|2018|1100|
banana| |2900|
apple | |3000|
|2018|2900|
|2019|3000|
ROLLUP ( c1, c2, c3 )
GROUPING SETS (
( c1, c2, c3 ),
( e1, e2 ),
( e1 ),
( )
)
SELECT item, year, SUM(quantity)
FROM sales
GROUP BY ROLLUP (item,year);
item |year|sum |
------|----|----|
| |5900|
banana|2019|1800|
apple |2019|1200|
apple |2018|1800|
banana|2018|1100|
banana| |2900|
apple | |3000|
SELECT coalesce(item, '所有产品') AS "产品",
coalesce(year, '所有年度') AS "年度",
SUM(quantity) as "销量"
FROM sales
GROUP BY ROLLUP (item,year);
产品 |年度 |销量 |
------|----|----|
所有产品|所有年度|5900|
banana |2019 |1800|
apple |2019 |1200|
apple |2018 |1800|
banana |2018 |1100|
banana |所有年度|2900|
apple |所有年度|3000|
SELECT coalesce(item, '所有产品') AS "产品",
coalesce(year, '所有年度') AS "年度",
SUM(quantity) as "销量"
FROM sales
GROUP BY item, ROLLUP (year);
产品 |年度 |销量 |
------|----|----|
banana |2019 |1800|
apple |2019 |1200|
apple |2018 |1800|
banana |2018 |1100|
banana |所有年度|2900|
apple |所有年度|3000|
CUBE ( (c1, c2), (c3, c4) )
GROUPING SETS (
( c1, c2, c3, c4 ),
( c1, c2 ),
( c3, c4 ),
( )
)
ROLLUP ( c1, (c2, c3), c4 )
GROUPING SETS (
( c1, c2, c3, c4 ),
( c1, c2, c3 ),
( c1 ),
( )
)
-- 未知产品在 2018 年的销量为 5000
INSERT INTO sales VALUES(NULL, '2018', 5000);
SELECT coalesce(item, '所有产品') AS "产品",
coalesce(year, '所有年度') AS "年度",
SUM(quantity) as "销量"
FROM sales
GROUP BY ROLLUP (item,year);
SELECT item AS "产品",
year AS "年度",
SUM(quantity) as "销量",
GROUPING(item),
GROUPING(year),
GROUPING(item, year)
FROM sales
GROUP BY ROLLUP (item,year);
SELECT CASE GROUPING(item) WHEN 1 THEN '所有产品' ELSE item END AS "产品",
CASE GROUPING(year) WHEN 1 THEN '所有年度' ELSE year END AS "年度",
SUM(quantity) as "销量"
FROM sales
GROUP BY ROLLUP (item,year);
原文作者:SQL编程思想;如有侵权,请联系管理员删除。