1. 程式人生 > >mysql求平均值

mysql求平均值

表結構: 

create table monitor
(
  instanceId        varchar(100) not null,
  date              date         not null,
  cloud             varchar(20)  not null,
  avgCpuUseRatio    float        null,
  primary key (instanceId, date, cloud)
);

 

#求instanceId對應的avgCpuUseRatio的平均值

select instanceId, AVG(avgCpuUseRatio) from monitor where cloud="Aliyun" and type="ecs" and date>='2018-07-01' and  date<= '2018-07-31' group by instanceId;

參考:

https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_avg

 

對於python的sqlalchemy可以使用text語法

>>> from sqlalchemy import create_engine
>>> engine = create_engine('sqlite:///:memory:', echo=True)
>>> conn = engine.connect()

 

 

 

>>> from sqlalchemy.sql import text

>>> s = text( ... "SELECT users.fullname || ', ' || addresses.email_address AS title " ... "FROM users, addresses " ... "WHERE users.id = addresses.user_id " ... "AND users.name BETWEEN :x AND :y " ... "AND (addresses.email_address LIKE :e1 " ... "OR addresses.email_address LIKE :e2)")

SQL

>>> conn.execute(s, x='m', y='z', e1='%@aol.com', e2='%@msn.com').fetchall() [(u'Wendy Williams, [email protected]',)]

 

 

 

參考:http://docs.sqlalchemy.org/en/latest/core/tutorial.html#using-textual-sql