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, wendy@aol.com',)]

 

 

 

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

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值