select
sum(salary) as sal_sum,
round(avg(salary)) as sal_avg,
min(salary) as sal_min,
max(salary) as sal_max,
count(*) as rec_count,
count(bonus) as bonus_count
from employee;
Into different groups
According to grouping criterion, here OUID
Application of aggregation functions on each individual group
select
sum(salary) as sal_sum,
round(avg(salary)) as sal_avg,
min(salary) as sal_min,
max(salary) as sal_max,
count(*) as rec_count,
count(bonus) as bonus_count
from employee
group by ouid;
One record per group in the output
select ouid,
sum(salary) as sal_sum
from employee
group by ouid
order by ouid;
All output columns on same aggregation level
select ouid, lastname,
sum(salary) as sal_sum
from employee
group by ouid
order by ouid;
Output columns on different aggregation levels
select ou.name,
sum(salary) as sal_sum,
count(*) as rec_count
from employee e
join orgunit ou on ou.ouid=e.ouid
group by ou.name
order by ou.name;
select
extract(year from hiredate) as hireyear,
sum(salary) as sal_sum,
count(*) as rec_count
from employee
group by hireyear
order by hireyear;
select
case
when hiredate >= '2010-01-01' then '2010-from'
when hiredate >= '2000-01-01' then '2009-until'
end as entry_year_range,
sum(salary) as sal_sum,
count(*) as rec_count
from employee
group by entry_year_range
order by entry_year_range;
select
ouid,
case
when hiredate >= '2010-01-01' then '2010-from'
when hiredate >= '2000-01-01' then '2009-until'
end as entry_year_range,
sum(salary) as sal_sum,
count(*) as rec_count
from employee
group by ouid, entry_year_range
order by ouid, entry_year_range;
order by entry_year_range;
select
case
when hiredate >= '2010-01-01' then '2010-from'
when hiredate >= '2000-01-01' then '2009-until'
end as entry_year_range,
sum(salary) as sal_sum,
count(*) as rec_count
from employee
group by entry_year_range
order by entry_year_range;
select
ouid,
case
when hiredate >= '2010-01-01' then '2010-from'
when hiredate >= '2000-01-01' then '2009-until'
end as entry_year_range,
sum(salary) as sal_sum,
count(*) as rec_count
from employee
group by ouid, entry_year_range
order by ouid, entry_year_range;
Adding a column in grouping usually leads to new groups
Select ou.ouid, ou.name, sum(salary) as sal_sum
from employee e
join orgunit ou on ou.ouid=e.ouid
group by ou.ouid
order by ou.ouid;
Every time OUID has the same value, also name has the same value
Column ou.name added to group by
No new combinations, because ou.name depends on ou.OUID
select ou.ouid, ou.name, sum(salary) as sal_sum
from employee e
join orgunit ou on ou.ouid=e.ouid
group by ou.ouid, ou.name
order by ou.ouid;
select ouid,
sum(salary) as sal_sum,
count(*) as rec_count
from employee
group by ouid
order by ouid;
Keyword having
select ouid,
sum(salary) as sal_sum,
count(*) as rec_count
from employee
group by ouid
having count(*) > 1
order by ouid;
select ouid,
sum(salary) as sal_sum,
count(*) as rec_count
from employee
group by ouid
having count(*) > 1
order by ouid;
Keyword where
select ouid,
sum(salary) as sal_sum,
count(*) as rec_count
from employee
where salary > 40000
group by ouid
having count(*) > 1
order by ouid;
select
from
where
group by
having
order by