MySQL, SQLServer, Oracle packet ordering

  1. MySQL:

1. aggregate functions: max (), min () , avg (), sum (), count ()
on the packet accumulation function.
2. Polymerization function data corresponding to each group: i.e., max (id) in each group greatest ID
3.having is the group by grouping together the result set of conditions (corresponding to HAVING Where but only after the group by, conditions aggregation function may be a (real table data may not be present)).

E.g:

SELECT
    * 
FROM
    tb_chatlog 
WHERE
    chatid IN ( SELECT max( Chatid ) maxId FROM tb_chatlog WHERE LoginUser = '*******' GROUP BY LoginUser, BuyerName );

select can occur after any field, not the field have to be grouped.

More packet field, the less data, packet ordering usually acquires a value (maximum value id) in the group after sorting, it can be reasonably assigned and packet field id set maximum aggregate functions, then according to subqueries obtain the required result set.

  2. SQLServer:

SQLserver grouping queries:

Function: ROW_NUMBER () over

SQLserver in: group by the field, can occur in the select, group by not only the fields used in aggregate functions select.

SELECT
    * 
FROM
    (
SELECT
    ROW_NUMBER ( ) over ( PARTITION BY LoginUser, BuyerName ORDER BY ChatTime DESC ) rowNums,* 
FROM
    tb_chatlog 
WHERE
    LoginUser = '丰达盛伟服饰专营店' 
    AND ChatTime >= '2019-07-07 00:00' 
    AND ChatTime <= '2019-07-17 23:59' 
    AND Direction = '0' 
    ) subGroup 
WHERE
    rowNums = '1';

3. Oracle:

 

Guess you like

Origin www.cnblogs.com/erlongxizhu-03/p/11248401.html