1GROUP BY: is used to collaborate
2with the SELECT statement to arrange
3matching data into groups.
4
5ORDER BY: is for sorting result
6either in descending or ascending order.
1--- GROUP FUNCTION | MULTI ROW FUNCTION | AGGREGATE FUNCTION
2--- COUNT , MAX , MIN , SUM , AVG
1 SELECT column_name(s)
2 FROM table_name
3 WHERE condition
4 GROUP BY column_name(s)
5 HAVING condition
6 ORDER BY column_name(s);
1/*Group by clause is used to group a selected set of rows into a set of summary
2rows by the values of one or more column or expression. It is always used in
3Conjunction with one or more aggregate function.*/
4SELECT AGGREGATE_FUNCTION(Column_Name) FROM Table_Name
5/*Group by exmaple*/
6SELECT city, sum(Salary) as TotalSalary FROM tblEmployee GROUP BY City
1class groupby(object):
2 # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
3 # [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
4 def __init__(self, iterable, key=None):
5 if key is None:
6 key = lambda x: x
7 self.keyfunc = key
8 self.it = iter(iterable)
9 self.tgtkey = self.currkey = self.currvalue = object()
10 def __iter__(self):
11 return self
12 def next(self):
13 while self.currkey == self.tgtkey:
14 self.currvalue = next(self.it) # Exit on StopIteration
15 self.currkey = self.keyfunc(self.currvalue)
16 self.tgtkey = self.currkey
17 return (self.currkey, self._grouper(self.tgtkey))
18 def _grouper(self, tgtkey):
19 while self.currkey == tgtkey:
20 yield self.currvalue
21 self.currvalue = next(self.it) # Exit on StopIteration
22 self.currkey = self.keyfunc(self.currvalue)
23