
IT/파이썬 2023. 5. 3.
Django ORM - Group by 사용해서 Count, Sum, Avg 구하기
Django ORM으로 Group by 필드 구현하는 방법은 아래 글을 이길 수가 없어요. 완전 잘 써놓으셔서 들어가서 읽고 감탄만 하시길!! 정리해 보면, 1. 먼저 Group by 할 필드를 정해서 values에 넣고 .values("필드") 2. 함수를 불러서 쓰고 .annotate(Count(), Sum(), Avg(), ...) 3. 보여줄 필드를 다시 정리하고 .values("필드", "필드", ...) 즉, values 를 2번 불러 쓰면 된다. [Django] ORM으로 Aggregation 함수와 Group by 사용 [Django] ORM으로 Aggregation 함수와 Group by 사용 쿼리에서 aggregation함수(count(), max(), first() 등등) 와 필요한 ..

IT/DB 2021. 6. 15.
mongodb - Flask mongoengine 필드 동일값을 그룹으로 묶기
저장된 데이터를 특정한 필드 값으로 그룹을 묶고 싶을때가 있다. 같은 값을 가진 문서가 몇개인지 궁금할 때가 가장 단순한 예가 되겠다. 이런 경우 aggregate 함수를 활용하면 된다. 몽고db 매뉴얼 페이지에서 해당 내용을 가져와 보자. https://docs.mongodb.com/manual/reference/operator/aggregation/sum/ Grouping the documents by the day and the year of the date field, the following operation uses the $sum accumulator to compute the total amount and the count for each group of documents. db.sales..
