from sqlalchemy import func, desc
# 根据聚合查询总收入,按总收入逆序
s= db.session.query(TpOrders.room_type_id, (func.sum(TpOrders.real_income)).label('total_real_income'), func.count(TpOrders.id)).group_by(TpOrders.room_type_id).order_by( desc('total_real_income')).all()
# 根据日期(年月日,忽略时分秒)进行分组统计查询
date_income_query = db.session.query(func.date_format(TpOrders.check_out_date, '%Y-%m-%d').label('date'), func.sum(TpOrders.real_income),func.count(TpOrders.id)).group_by('date').all()