Şöyle yaparız
SQL olarak şöyledir> db.car.aggregate([... { $group: { _id: "$make", avg_price: { $avg: "$price" }}}... ]){ "_id" : "hyundai", "avg_price" : 36333.333333333336 }{ "_id" : "BMW", "avg_price" : 47400 }{ "_id" : "ford", "avg_price" : 35333.333333333336 }
mysql> select make, avg(price) -> from car -> group by make; +---------+------------+ | make | avg(price) | +---------+------------+ | BMW | 47400.0000 | | ford | 35333.3333 | | hyundai | 36333.3333 | +---------+------------+Örnek
Şöyle yaparız. Yukarıdaki örnek ile aynı. Sadece 2019 modeller seçiliyor.
> db.car.aggregate([... { $match: { year: "2019" }}, ... { $group: { _id: "$make", avg_price: { $avg: "$price" }}} ... ]) { "_id" : "BMW", "avg_price" : 53000 } { "_id" : "ford", "avg_price" : 42000 } { "_id" : "hyundai", "avg_price" : 41000 }
SQL olarak şöyledir
mysql> select make, avg(price) -> from car -> where year = "2019" -> group by make; +---------+------------+ | make | avg(price) | +---------+------------+ | BMW | 53000.0000 | | ford | 42000.0000 | | hyundai | 41000.0000 | +---------+------------+
Hiç yorum yok:
Yorum Gönder