Top N and bottom N view on the same worksheet In Tableau
Understand SQL Group By with this concise guide
1. GraspSQL | SQL Group By
What does 'group by' do:
groups lines in a table
How:
lines with same value in one or more column goes in
the same group
Purpose:
display averages, sums, maximum values for each
group.
2. GraspSQL | SQL Group By
Quiz:
When grouping by state, how
many groups in this table
When grouping by city, how
many groups in this table
When grouping by revenue, how
many groups in this table
3. GraspSQL | SQL Group By
In the following example, we will show:
the sum of revenues earned per state.
the number of sales transactions per state.
the amount of the biggest transaction per state.
the amount of the smallest transaction per state.
the average sales transaction amount per state.
4. GraspSQL | SQL Group By
Example:
When grouping by state,
we select the state
The only other things
we can select
are aggregate functions
5. GraspSQL | SQL Group By
AVOID:
Selecting a column other than column used for
grouping without an aggregate function:
select state, city, sum(revenue)
from sales
group by state
6. GraspSQL | SQL Group By
Taking into account only sales from 2014
select state, sum(revenue)
from sales
where year=2014
group by state
7. GraspSQL | SQL Group By
What is 'where' doing:
Indicates which lines we take into consideration when
grouping (other lines are just ignored)
8. GraspSQL | SQL Group By
Showing group only if sum revenue for group >
50000
select state, sum(revenue)
from sales
group by state
having sum(revenue)>60000
9. GraspSQL | SQL Group By
What is 'having' doing:
Indicates which groups are shown, based on aggregate
function value for the group
10. GraspSQL | SQL Group By
Quiz:
When grouping by state:
To consider only revenues per city
over 10 000 do we use where
revenue = 10000 or having revenue
= 10000 or
To consider only states for which
the total revenue is over 10 000
do we use where revenue = 10000
or having revenue = 10000