Grouping data with postgresql
Grouping data with postgresql : [do read sql basics first ]
Group by clauses can contain multiple columns, Every column in your select statement must be present in a group by clause except for aggregated calculations. Also all Nulls are also grouped together in the mentioned column.
Grouping data with postgresql.
Having clause for filtering.
Where clause does not work for groups as it only works on rows.
instead use HAVING clause to filter for groups.
Where filters before the data is grouped and having function filters after the data is grouped.
Example :
--create a sample table
>> create table orders(customerID int,customerNAME text);
>>insert into orders values(23,'chandler');
>>insert into orders values(23,'chandler');
>>insert into orders values(23,'chandler');
>>insert into orders values(20,'ross');
>>insert into orders values(20,'ross');
>>insert into orders values(07,'joye');
>>insert into orders values(10,'phebe');
>>insert into orders values(10,'phebe');
>>insert into orders values(10,'phebe');
>>insert into orders values(10,'phebe');
>>insert into orders values(10,'phebe');
--OUTPUT
>>select customerID from orders group by customerID;
customerid
------------
10
7
20
23
(4 rows)
--so how group by works is it groups data into small groups , like in --the I case what i did was group the data by customerid and print it, so --the data was divided into 4 groups with separate customerID[7,10,20,23].
>>select customerID,customerNAME from orders group by customerid,customerNAME ;
customerid | customername
------------+--------------
20 | ross
7 | joye
23 | chandler
10 | phebe
(4 rows)
--so here in case II we asked sql to group by id and name so it made different groups with specific id and associated name, to test what happens when a entry is made with different name and same id which is certainly possible for two customers to have diff id but same name Case III.
>>insert into orders values(25,'ross');
>>select customerID,customerNAME from orders group by customerid,customerNAME ;
customerid | customername
------------+--------------
20 | ross
7 | joye
23 | chandler
25 | ross
10 | phebe
(5 rows)
Using Having clause - filter groups with occurrences more than once
>>select customerID,count(*) as orders from orders group by customerID HAVING COUNT(*) >=2;
customerid | orders
------------+--------
10 | 5
20 | 2
23 | 3
(3 rows)
--remember having filters data after it has been grouped but where works before grouping.
One more thing i would suggest is always use a order by with group by clause to get a well ordered data o/p,
example:
>>select customerID,count(*) as orders from orders group by customerID HAVING COUNT(*) >=2 order by customerID;
--remember order by always goes at the end.
Group by clauses can contain multiple columns, Every column in your select statement must be present in a group by clause except for aggregated calculations. Also all Nulls are also grouped together in the mentioned column.
Grouping data with postgresql.
Having clause for filtering.
Where clause does not work for groups as it only works on rows.
instead use HAVING clause to filter for groups.
Where filters before the data is grouped and having function filters after the data is grouped.
Example :
--create a sample table
>> create table orders(customerID int,customerNAME text);
>>insert into orders values(23,'chandler');
>>insert into orders values(23,'chandler');
>>insert into orders values(23,'chandler');
>>insert into orders values(20,'ross');
>>insert into orders values(20,'ross');
>>insert into orders values(07,'joye');
>>insert into orders values(10,'phebe');
>>insert into orders values(10,'phebe');
>>insert into orders values(10,'phebe');
>>insert into orders values(10,'phebe');
>>insert into orders values(10,'phebe');
--OUTPUT
>>select customerID from orders group by customerID;
customerid
------------
10
7
20
23
(4 rows)
--so how group by works is it groups data into small groups , like in --the I case what i did was group the data by customerid and print it, so --the data was divided into 4 groups with separate customerID[7,10,20,23].
>>select customerID,customerNAME from orders group by customerid,customerNAME ;
customerid | customername
------------+--------------
20 | ross
7 | joye
23 | chandler
10 | phebe
(4 rows)
--so here in case II we asked sql to group by id and name so it made different groups with specific id and associated name, to test what happens when a entry is made with different name and same id which is certainly possible for two customers to have diff id but same name Case III.
>>insert into orders values(25,'ross');
>>select customerID,customerNAME from orders group by customerid,customerNAME ;
customerid | customername
------------+--------------
20 | ross
7 | joye
23 | chandler
25 | ross
10 | phebe
(5 rows)
>>select customerID,count(*) as orders from orders group by customerID HAVING COUNT(*) >=2;
customerid | orders
------------+--------
10 | 5
20 | 2
23 | 3
(3 rows)
--remember having filters data after it has been grouped but where works before grouping.
One more thing i would suggest is always use a order by with group by clause to get a well ordered data o/p,
example:
>>select customerID,count(*) as orders from orders group by customerID HAVING COUNT(*) >=2 order by customerID;
--remember order by always goes at the end.
Comments
Post a Comment