[mongodb] SQL to Aggregation Mapping Chart 몽고디비 SQL 쿼리 매핑

 

SQL to Aggregation Mapping Chart

On this page

The aggregation pipeline allows MongoDB to provide native aggregation capabilities that corresponds to many common data aggregation operations in SQL.

The following table provides an overview of common SQL aggregation terms, functions, and concepts and the corresponding MongoDB aggregation operators:

SQL Terms, Functions, and Concepts MongoDB Aggregation Operators
WHERE $match
GROUP BY $group
HAVING $match
SELECT $project
ORDER BY $sort
LIMIT $limit
SUM() $sum
COUNT()
join $lookup
SELECT INTO NEW_TABLE $out
MERGE INTO TABLE $merge (Available starting in MongoDB 4.2)
UNION ALL $unionWith (Available starting in MongoDB 4.4)

For a list of all aggregation pipeline and expression operators, see Aggregation Pipeline Quick Reference.

SEE ALSO

SQL to MongoDB Mapping Chart

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

Examples

The following table presents a quick reference of SQL aggregation statements and the corresponding MongoDB statements. The examples in the table assume the following conditions:

  • The SQL examples assume two tables, orders and order_lineitem that join by the order_lineitem.order_id and the orders.id columns.

  • The MongoDB examples assume one collection orders that contain documents of the following prototype:

    {
      cust_id: "abc123",
      ord_date: ISODate("2012-11-02T17:04:11.102Z"),
      status: 'A',
      price: 50,
      items: [ { sku: "xxx", qty: 25, price: 1 },
               { sku: "yyy", qty: 25, price: 1 } ]
    }
    
SQL Example MongoDB Example Description
 
SELECT COUNT(*) AS count
FROM orders
 
db.orders.aggregate( [
   {
     $group: {
        _id: null,
        count: { $sum: 1 }
     }
   }
] )
Count all records from orders
 
SELECT SUM(price) AS total
FROM orders
 
db.orders.aggregate( [
   {
     $group: {
        _id: null,
        total: { $sum: "$price" }
     }
   }
] )
Sum the price field from orders
 
SELECT cust_id,
       SUM(price) AS total
FROM orders
GROUP BY cust_id
 
db.orders.aggregate( [
   {
     $group: {
        _id: "$cust_id",
        total: { $sum: "$price" }
     }
   }
] )
For each unique cust_id, sum the price field.
 
SELECT cust_id,
       SUM(price) AS total
FROM orders
GROUP BY cust_id
ORDER BY total
 
db.orders.aggregate( [
   {
     $group: {
        _id: "$cust_id",
        total: { $sum: "$price" }
     }
   },
   { $sort: { total: 1 } }
] )
For each unique cust_id, sum the price field, results sorted by sum.
 
SELECT cust_id,
       ord_date,
       SUM(price) AS total
FROM orders
GROUP BY cust_id,
         ord_date
 
db.orders.aggregate( [
   {
     $group: {
        _id: {
           cust_id: "$cust_id",
           ord_date: { $dateToString: {
              format: "%Y-%m-%d",
              date: "$ord_date"
           }}
        },
        total: { $sum: "$price" }
     }
   }
] )
For each unique cust_idord_date grouping, sum the price field. Excludes the time portion of the date.
 
SELECT cust_id,
       count(*)
FROM orders
GROUP BY cust_id
HAVING count(*) > 1
 
db.orders.aggregate( [
   {
     $group: {
        _id: "$cust_id",
        count: { $sum: 1 }
     }
   },
   { $match: { count: { $gt: 1 } } }
] )
For cust_id with multiple records, return the cust_id and the corresponding record count.
 
SELECT cust_id,
       ord_date,
       SUM(price) AS total
FROM orders
GROUP BY cust_id,
         ord_date
HAVING total > 250
 
db.orders.aggregate( [
   {
     $group: {
        _id: {
           cust_id: "$cust_id",
           ord_date: { $dateToString: {
              format: "%Y-%m-%d",
              date: "$ord_date"
           }}
        },
        total: { $sum: "$price" }
     }
   },
   { $match: { total: { $gt: 250 } } }
] )
For each unique cust_idord_date grouping, sum the price field and return only where the sum is greater than 250. Excludes the time portion of the date.
 
SELECT cust_id,
       SUM(price) as total
FROM orders
WHERE status = 'A'
GROUP BY cust_id
 
db.orders.aggregate( [
   { $match: { status: 'A' } },
   {
     $group: {
        _id: "$cust_id",
        total: { $sum: "$price" }
     }
   }
] )
For each unique cust_id with status A, sum the price field.
 
SELECT cust_id,
       SUM(price) as total
FROM orders
WHERE status = 'A'
GROUP BY cust_id
HAVING total > 250
 
db.orders.aggregate( [
   { $match: { status: 'A' } },
   {
     $group: {
        _id: "$cust_id",
        total: { $sum: "$price" }
     }
   },
   { $match: { total: { $gt: 250 } } }
] )
For each unique cust_id with status A, sum the price field and return only where the sum is greater than 250.
 
SELECT cust_id,
       SUM(li.qty) as qty
FROM orders o,
     order_lineitem li
WHERE li.order_id = o.id
GROUP BY cust_id
 
db.orders.aggregate( [
   { $unwind: "$items" },
   {
     $group: {
        _id: "$cust_id",
        qty: { $sum: "$items.qty" }
     }
   }
] )
For each unique cust_id, sum the corresponding line item qty fields associated with the orders.
 
SELECT COUNT(*)
FROM (SELECT cust_id,
             ord_date
      FROM orders
      GROUP BY cust_id,
               ord_date)
      as DerivedTable
 
db.orders.aggregate( [
   {
     $group: {
        _id: {
           cust_id: "$cust_id",
           ord_date: { $dateToString: {
              format: "%Y-%m-%d",
              date: "$ord_date"
           }}
        }
     }
   },
   {
     $group: {
        _id: null,
        count: { $sum: 1 }
     }
   }
] )
Count the number of distinct cust_idord_date groupings. Excludes the time portion of the date.
 

[출처] https://docs.mongodb.com/manual/reference/sql-aggregation-comparison/

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
공지 오라클 기본 샘플 데이터베이스 졸리운_곰 2014.01.02 25085
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 24564
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 25943
805 MongoDB : 기본 구조 file 졸리운_곰 2020.09.30 23
804 [tensorflow] 텐서플로로 음악 작곡 : Generate Music in TensorFlow 졸리운_곰 2020.09.27 57
803 한국어 BERT, HanBert를 소개합니다. file 졸리운_곰 2020.09.25 421
» [mongodb] SQL to Aggregation Mapping Chart 몽고디비 SQL 쿼리 매핑 졸리운_곰 2020.09.23 95
801 한국어 자연어 처리 데이터셋 목록 file 졸리운_곰 2020.09.20 695
800 초간단 Mongo DB Quick Start Guide file 졸리운_곰 2020.09.20 19
799 Getting started with Apache Kafka in Python file 졸리운_곰 2020.09.10 58
798 데이터 입력/수정/삭제를 한 번에 처리할 수 있는 MERGE에 대해 알아보자 file 졸리운_곰 2020.09.10 63
797 Oracle :: MERGE INTO 졸리운_곰 2020.09.10 19
796 오라클 MERGE INTO - 한번에 INSERT, UPDATE 하기 졸리운_곰 2020.09.10 56
795 Oracle Merge 명령어 사용 file 졸리운_곰 2020.09.10 16
794 Google Colab 사용하기 file 졸리운_곰 2020.09.08 2120
793 [Kafka] 다운로드 및 Quick Start file 졸리운_곰 2020.09.07 35
792 [Kafka] 기본 개념잡기 file 졸리운_곰 2020.09.07 29
791 설문 조사를위한 데이터베이스 설계 file 졸리운_곰 2020.09.05 3363
790 Deep Learning (Keras) Models Deployment using SQL databases file 졸리운_곰 2020.08.04 45
789 Deep Learning (Keras) Models Deployment using SQL databases 졸리운_곰 2020.08.04 93
788 ubuntu에서 pytorch gpu 버전 설치 과정 정리 file 졸리운_곰 2020.07.28 33
787 [dbguide] 프로세스모델링 작성 절차 (데이터베이스 시스템분석설계 프로세스 모델링) file 졸리운_곰 2020.07.27 312
786 [dbguide] 데이터모델링 작성 절차 (데이터베이스 모델링 절차) file 졸리운_곰 2020.07.27 302
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED