갈루아의 반서재

728x90

sum, max 등 group by 를 이용한 결과를 표현할 때


a.py


q = session.query(
        included_parts.c.sub_part,
        func.sum(included_parts.c.quantity).label('total_quantity')
    ).\
    group_by(included_parts.c.sub_part)
a.html
{% for record in records %}
                <tr>
                  <td class="valign-middle">{{ loop.index }}</td>
                  <td class="valign-middle">{{ record.included_parts.c.sub_part}}</td>
                  <td class="valign-middle">{{ record.total_quantity}}</td>
                </tr>
     {% endfor %}

참조 : http://docs.sqlalchemy.org/en/rel_0_7/orm/query.html

 

728x90