SQLite 数据库 聚合函数案例

SQLite 数据库阿木 发布于 5 天前 1 次阅读


摘要:

本文将围绕SQLite数据库的聚合函数进行深入探讨,通过实际案例展示如何使用聚合函数进行数据统计和分析。我们将从基础概念入手,逐步深入到具体的代码实现,帮助读者更好地理解和应用SQLite的聚合函数。

一、

SQLite是一款轻量级的数据库管理系统,广泛应用于嵌入式系统、移动应用和桌面应用程序。在数据处理和分析中,聚合函数是必不可少的工具。本文将详细介绍SQLite中的聚合函数,并通过实际案例展示其应用。

二、SQLite 聚合函数概述

SQLite提供了丰富的聚合函数,用于对数据库中的数据进行统计和汇总。常见的聚合函数包括:

1. COUNT()

2. SUM()

3. AVG()

4. MIN()

5. MAX()

以下是对这些函数的简要介绍:

- COUNT():计算指定列中非NULL值的数量。

- SUM():计算指定列中所有值的总和。

- AVG():计算指定列中所有值的平均值。

- MIN():返回指定列中的最小值。

- MAX():返回指定列中的最大值。

三、案例解析

为了更好地理解聚合函数的应用,以下将通过几个案例进行解析。

案例一:计算员工总数

假设我们有一个名为`employees`的表,其中包含员工的姓名和年龄。我们想计算该表中员工的总数。

sql

SELECT COUNT() FROM employees;


案例二:计算部门总工资

假设我们有一个名为`departments`的表,其中包含部门名称和部门总工资。我们想计算每个部门的总工资。

sql

SELECT department_name, SUM(salary) AS total_salary


FROM departments


GROUP BY department_name;


案例三:计算平均年龄

假设我们有一个名为`students`的表,其中包含学生的姓名和年龄。我们想计算所有学生的平均年龄。

sql

SELECT AVG(age) AS average_age


FROM students;


案例四:查找最小和最大年龄

假设我们有一个名为`students`的表,其中包含学生的姓名和年龄。我们想查找该表中年龄的最小值和最大值。

sql

SELECT MIN(age) AS min_age, MAX(age) AS max_age


FROM students;


四、代码实现

以下是对上述案例的代码实现:

python

import sqlite3

连接到SQLite数据库


conn = sqlite3.connect('example.db')


cursor = conn.cursor()

创建表


cursor.execute('''


CREATE TABLE IF NOT EXISTS employees (


name TEXT,


age INTEGER


)


''')

cursor.execute('''


CREATE TABLE IF NOT EXISTS departments (


department_name TEXT,


total_salary REAL


)


''')

cursor.execute('''


CREATE TABLE IF NOT EXISTS students (


name TEXT,


age INTEGER


)


''')

插入数据


cursor.execute("INSERT INTO employees (name, age) VALUES ('Alice', 30)")


cursor.execute("INSERT INTO employees (name, age) VALUES ('Bob', 25)")


cursor.execute("INSERT INTO employees (name, age) VALUES ('Charlie', 35)")

cursor.execute("INSERT INTO departments (department_name, total_salary) VALUES ('HR', 50000)")


cursor.execute("INSERT INTO departments (department_name, total_salary) VALUES ('Tech', 75000)")


cursor.execute("INSERT INTO departments (department_name, total_salary) VALUES ('Sales', 60000)")

cursor.execute("INSERT INTO students (name, age) VALUES ('David', 20)")


cursor.execute("INSERT INTO students (name, age) VALUES ('Eve', 22)")


cursor.execute("INSERT INTO students (name, age) VALUES ('Frank', 19)")

执行聚合函数查询


cursor.execute("SELECT COUNT() FROM employees")


print("Total number of employees:", cursor.fetchone()[0])

cursor.execute("SELECT department_name, SUM(total_salary) AS total_salary FROM departments GROUP BY department_name")


print("Total salary by department:")


for row in cursor.fetchall():


print(row)

cursor.execute("SELECT AVG(age) AS average_age FROM students")


print("Average age of students:", cursor.fetchone()[0])

cursor.execute("SELECT MIN(age) AS min_age, MAX(age) AS max_age FROM students")


print("Minimum and maximum age of students:", cursor.fetchone())

提交事务


conn.commit()

关闭连接


cursor.close()


conn.close()


五、总结

本文通过介绍SQLite数据库的聚合函数,并通过实际案例展示了其应用。通过学习本文,读者可以更好地理解和应用SQLite的聚合函数,从而在数据处理和分析中发挥重要作用。

注意:以上代码示例仅供参考,实际应用中可能需要根据具体需求进行调整。