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);
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
1SELECT <field1, field2, field3…>
2FROM <table1_name>
3WHERE <condition/expression>
4GROUP BY <field1, field2, field3…>