sql group by
1.group by
The group by statement is used to group the result-set by one or more columns, e.g.
if the sql is : select rate_type, effective_date from table group by rate_type, effective_date, then the result looks like next :
SC 02/10/15
SC 12/08/16
Note : the columns in select have to be in group by or in aggregate functions, in other words, if we want to select rate_type, effective_date, approve_time in a sql with group by, then the sql should be :
‘select rate_type, effective_date, approve_time from table group by rate_type,effective_date,approve_time’
or
‘select rate_type, effective_code, max(approve_time) from table group by rate_type,effective_date’
Note : the column with varchar type also can be used in aggregate functions besides the number
and it is wrong if it is ‘select rate_type, effective_date, approve_time from table group by rate_type,effective_date’.
2.having
The having clause is used with group by usually and the aggregate functions following it.
It was added to sql because where clause could not be used with aggregate functions.