Professional Documents
Culture Documents
Ms
Siguiente blog
Crear blog
Acceder
Blog Archive
2010 (19)
December (4)
CREATE FUNCTION: return
types
CREATE FUNCTION - basic
anatomy
schema
Aggregates
November (15)
About Me
Notice that this is returning just one record when in fact the query would produce multiple
rows of results.
FUNCTIONS THAT RETURN A SETOF VALUES
http://www.postgresqlforbeginners.com/
Carl Anderson
Director of Data Science at
WarbyParker.com. Follow me on
1/18
16/4/2014
twitter
@leapingllamas.
My linkedin profile
View my complete
profile
http://www.postgresqlforbeginners.com/
2/18
16/4/2014
5 comments:
http://www.postgresqlforbeginners.com/
3/18
16/4/2014
http://www.postgresqlforbeginners.com/
4/18
16/4/2014
5 comments:
schema
In this post we'll examine PostgreSQL's schema support.
We encountered the default schema called "public" in an earlier post when exploring
pgAdmin. Colloquially, when someone talks about a database's schema they typically
mean the structure of the tables (that this table has these fields with these types). Here, a
schema is a higher level of organization, a container or namespace within a database. A
schema contains a set of tables, views, stored procedures and triggers and so on, all the
usual components of a database. However, one can have multiple schema within a
PostgreSQL database. To clarify this, let's start with a concrete example and consider the
pros and cons of different ways of organizing the data.
Suppose we have a supermarket company, SuperFoods Inc., who have two stores, say
Sausalito, CA and Petaluma, CA. (Clearly they have recovered from the bad economy and
layoffs of this earlier post.) Each of the two stores has a set of information: employees
and contact details, details about the physical store itself, replenishment orders and order
history etc.
Imagine the data are stored in two separate databases. The CEO of SuperFoods asks one
of his minions to produce a report on the sales history of the two stores. The problem here
http://www.postgresqlforbeginners.com/
5/18
16/4/2014
http://www.postgresqlforbeginners.com/
6/18
16/4/2014
http://www.postgresqlforbeginners.com/
8 comments:
7/18
16/4/2014
Aggregates
PostgreSQL supports a number of different aggregate functions (a tutorial appears here).
These are functions that perform some mathematical or statistical calculations on a set of
values. Common examples are sum, min, max and count. The values are often numeric
but they can be boolean, string, or XML too and count accepts anything.
Let's start with a simple example. Suppose we have the following table of sales per month
for different sales people
aggs=# select * from salesfigures;
name | month | sales
------+-------+------joe | jan |
34
joe | feb |
23
bob | jan |
18
bob | feb |
30
(4 rows)
We can get the total sales for the sales team for the year to date as
aggs=# select sum(sales) from salesfigures;
sum
----105
(1 row)
Notice that sum is a function that we can specify in the select list (the part before the
FROM) and it takes one argument. We are supplying it the values in the sales column. It
adds 34 + 23 + 18 + 30 to get 105. Easy.
Filtering by WHERE
As the aggregate gets computed after any WHERE clause is applied we can easily apply
filters on the data that is supplied to the aggregate. For instance, to get the sales for
January
aggs=# select sum(sales) from salesfigures where month='jan';
sum
----52
(1 row)
(34 + 18 = 52)
Filtering by GROUP
Similarly, aggregates are computed after GROUP BY clauses. This is a really powerful
feature. So, if we want the total sales for each month we can run
aggs=# select month, sum(sales) from salesfigures group by month;
month | sum
-------+----feb | 53
jan | 52
(2 rows)
Clearly, using strings for month is not the best table design here as they cannot sensibly
be ordered or compared. I used them to keep the example simple.
We call the aggregates multiple times in the select list. Here we get the min and max
sales values for each person
aggs=# select name, min(sales), max(sales) from salesfigures
http://www.postgresqlforbeginners.com/
8/18
16/4/2014
WRONG!
We could imagine this as computing the maximum value in the sales column and the
selecting the one or more names that matched this value. This will not work. If one runs
this query, PostgreSQL complains
aggs=# select name from salesfigures where sales=max(sales);
ERROR: aggregates not allowed in WHERE clause
LINE 1: select name from salesfigures where sales=max(sales);
^
aggs=#
This is a typical beginner mistake. I've done it. You'll probably do it too. As the error
message says, aggregates are not allowed in the WHERE clause. This is because the
WHERE clause is used to generate the set of rows to be passed to the select list and
any aggregates. What we have here is a circular relationship: max depends on which rows
it is given and that is determined by WHERE, which depends on max... What you have to
do is obtain the maximum value as one query and then pass that into another query.
aggs=# select name from salesfigures where sales=(select
max(sales) from salesfigures);
name
-----joe
(1 row)
(select max(sales) from salesfigures)is called a subquery, something we
will cover in more detail in a later post. For now, just know that it computes this subquery
first to obtain a value, which will be 34. We then effectively have a simple query
select name from salesfigures where sales=34;
which is OK as the circularity has been eliminated.
Filtering by HAVING
OK, so we cannot have aggregates in WHERE clause. However, we can have them in a
HAVING clause. If you recall our earlier post, WHERE can be accompanied by an
optional HAVING clause that can be used to filter rows after the WHERE clause is
performed.
Who is underperforming? Which salesperson(s) have had less than 20 sales in any one
month?
aggs=# select name from salesfigures group by name having
min(sales)<20;
name
-----bob
(1 row)
This query groups first to get a set of virtual tables, then applies the min aggregate to get
the minumum per person. It then filters in those with a minumum less than 20. Finally, we
asked for SELECT name so it returns the one name left, that for Bob.
COUNT
COUNT is commonly used aggregate. SELECT count(*) from tablenamereturns
the number of rows. The * just means "everything", every column in this context. If we ran
http://www.postgresqlforbeginners.com/
9/18
16/4/2014
No comments:
http://www.postgresqlforbeginners.com/
10/18
16/4/2014
http://www.postgresqlforbeginners.com/
11/18
16/4/2014
5 comments:
http://www.postgresqlforbeginners.com/
12/18
16/4/2014
http://www.postgresqlforbeginners.com/
13/18
16/4/2014
http://www.postgresqlforbeginners.com/
14/18
16/4/2014
2 comments:
http://www.postgresqlforbeginners.com/
15/18
16/4/2014
http://www.postgresqlforbeginners.com/
16/18
16/4/2014
http://www.postgresqlforbeginners.com/
17/18
16/4/2014
3 comments:
Home
Older Posts
2010 Carl Anderson. All Rights Reserved. Simple template. Powered by Blogger.
http://www.postgresqlforbeginners.com/
18/18