GBASE南大通用GBase 8s数据库常用函数-聚合函数

GBASE南大通用分享:GBase 8s数据库常用函数

聚合函数

数据准备
drop table if exists t_dept;

create table t_dept(f_deptid int, f_deptname varchar(50));

insert into t_dept values(1, 'Dev');
insert into t_dept values(2, 'Test');
insert into t_dept values(3, 'Market');

drop table if exists t_employee;

create table t_employee(f_employeeid int, f_deptid int, f_employeename varchar(50), f_salary money);

insert into t_employee values(1, 1, 'Tom', 6000.00);
insert into t_employee values(2, 1, 'Jack', 8000.00);
insert into t_employee values(3, 1, 'Mary', 6600.00);

insert into t_employee values(4, 2, 'Henry', 5000.00);
insert into t_employee values(5, 2, 'Rose', 7500.00);
insert into t_employee values(6, 2, 'Bill', 6500.00);

insert into t_employee values(7, 3, 'Kate', 5000.00);
insert into t_employee values(8, 3, 'Bob', 9000.00);
COUNT
select a.f_deptid, b.f_deptname, count(1) as f_cnt from t_employee a, t_dept b where a.f_deptid = b.f_deptid group by a.f_deptid, b.f_deptname order by a.f_deptid;
> select a.f_deptid, b.f_deptname, count(1) as f_cnt from t_employee a, t_dept b where a.f_deptid = b.f_deptid group by a.f_deptid, b.f_deptname order by a.f_deptid;

f_deptid    1
f_deptname  Dev
f_cnt       3

f_deptid    2
f_deptname  Test
f_cnt       3

f_deptid    3
f_deptname  Market
f_cnt       2

3 row(s) retrieved.

> 

SUM

select a.f_deptid, b.f_deptname, sum(f_salary) as f_salary from t_employee a, t_dept b where a.f_deptid = b.f_deptid group by a.f_deptid, b.f_deptname order by a.f_deptid;
> select a.f_deptid, b.f_deptname, sum(f_salary) as f_salary from t_employee a, t_dept b where a.f_deptid = b.f_deptid group by a.f_deptid, b.f_deptname order by a.f_deptid;

f_deptid    1
f_deptname  Dev
f_salary    $20600.00

f_deptid    2
f_deptname  Test
f_salary    $19000.00

f_deptid    3
f_deptname  Market
f_salary    $14000.00

3 row(s) retrieved.

> 
GBASE南大通用分享:AVG
select a.f_deptid, b.f_deptname, avg(f_salary) as f_salary_avg from t_employee a, t_dept b where a.f_deptid = b.f_deptid group by a.f_deptid, b.f_deptname order by a.f_deptid;
> select a.f_deptid, b.f_deptname, avg(f_salary) as f_salary_avg from t_employee a, t_dept b where a.f_deptid = b.f_deptid group by a.f_deptid, b.f_deptname order by a.f_deptid;

f_deptid      1
f_deptname    Dev
f_salary_avg  $6866.67

f_deptid      2
f_deptname    Test
f_salary_avg  $6333.33

f_deptid      3
f_deptname    Market
f_salary_avg  $7000.00

3 row(s) retrieved.

> 
GBASE南大通用分享:MAX/MIN
select a.f_deptid, b.f_deptname, max(f_salary) as f_salary_max, min(f_salary) as f_salary_min from t_employee a, t_dept b where a.f_deptid = b.f_deptid group by a.f_deptid, b.f_deptname order by a.f_deptid;
> select a.f_deptid, b.f_deptname, max(f_salary) as f_salary_max, min(f_salary) as f_salary_min from t_employee a, t_dept b where a.f_deptid = b.f_deptid group by a.f_deptid, b.f_deptname order by a.f_deptid;

f_deptid      1
f_deptname    Dev
f_salary_max  $8000.00
f_salary_min  $6000.00

f_deptid      2
f_deptname    Test
f_salary_max  $7500.00
f_salary_min  $5000.00

f_deptid      3
f_deptname    Market
f_salary_max  $9000.00
f_salary_min  $5000.00

3 row(s) retrieved.

> 
GBASE南大通用分享:WM_CONCAT
select a.f_deptid, b.f_deptname, wm_concat(f_employeename) as f_employees from t_employee a, t_dept b where a.f_deptid = b.f_deptid group by a.f_deptid, b.f_deptname order by a.f_deptid;
> select a.f_deptid, b.f_deptname, wm_concat(f_employeename) as f_employees from t_employee a, t_dept b where a.f_deptid = b.f_deptid group by a.f_deptid, b.f_deptname order by a.f_deptid;


f_deptid     1
f_deptname   Dev
f_employees  Tom,Jack,Mary 

f_deptid     2
f_deptname   Test
f_employees  Henry,Rose,Bill 

f_deptid     3
f_deptname   Market
f_employees  Kate,Bob 

3 row(s) retrieved.

> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值