DOCS : https://www.sqlite.org/lang_aggfunc.html
함수 |
설명 |
avg(x) |
평균값 |
max(x) |
최대값 |
min(x) |
최소값 |
sum(x), total(x) |
총합 |
abs(x) |
절대값 |
count(x) |
Null이 아닌 튜플의 갯수 |
count(*) |
조회결과 튜플의 갯수 |
lenth(x) |
문자열의 길이 |
lower(x) |
입력받은 문자열을 소문자 변환 |
upper(x) |
입력받은 문자열을 대문자 변환 |
1. point 필드의 평균값, 최대값, 최소값
import sqlite3
conn = sqlite3.connect(':memory:')
cur = conn.cursor()
cur.execute('CREATE TABLE Score(nation, point)')
value = (('kr',90),('jp',80),('cn',85),('us',80))
cur.executemany('INSERT INTO Score VALUES(?,?)', value)
cur.execute('select avg(point), max(point), min(point) from Score')
for row in cur:
print(row)
(83.75, 90, 80)
2. point 필드의 합계
import sqlite3
conn = sqlite3.connect(':memory:')
cur = conn.cursor()
cur.execute('CREATE TABLE Score(nation, point)')
value = (('kr',90),('jp',80),('cn',85),('us',80))
cur.executemany('INSERT INTO Score VALUES(?,?)', value)
cur.execute('select sum(point), total(point) from Score')
for row in cur:
print(row)
(335, 335.0)
3. 문자열의 길이, 대문자 변환, 소문자 변환
import sqlite3
conn = sqlite3.connect(':memory:')
cur = conn.cursor()
cur.execute('CREATE TABLE Score(nation, point)')
value = (('kR',90),('jP',80),('cN',85),('uS',80))
cur.executemany('INSERT INTO Score VALUES(?,?)', value)
cur.execute('select length(nation),upper(nation),lower(nation) from Score')
for row in cur:
print(row)
(2, 'KR', 'kr')
(2, 'JP', 'jp')
(2, 'CN', 'cn')
(2, 'US', 'us')
'Python > SQLite' 카테고리의 다른 글
sqlitestudio download (0) | 2019.01.24 |
---|---|
[Python] sqlite3 iterdump( ) - SQL 쿼리 형식으로 데이터베이스를 덤프 (0) | 2019.01.02 |
[Python] SQLite3 distinct 중복제거 (0) | 2018.08.15 |
[Python] SQLite3 Cursor.executemany() - iterator를 이용한 레코드 추가 (0) | 2018.05.18 |
[Python] API SQLite (0) | 2018.04.23 |