Q:

Retrieve from firebase database calculating average

I'm creating an iOS application that uses placenames and rating for each place. I have already made the thing work. I mean that, I save data to my database and I also can read them. The only problem, is when I read them I want them to load on my tableviewcell, by calculating average for each place. See the screenshots and if you don't understand something, ask me to edit the answer. 
TableView

Firebase

My Code that loads data to tableview
import UIKit
import FirebaseDatabase

class PlacesTableViewController: UITableViewController {

    //MARK: Properties
    @IBOutlet weak var placesTableView: UITableView!
    var dbRef:FIRDatabaseReference?
    var places = [Places]()
    private var loadedLabels = [String: String]()
    private var loadedRatings = [String: Int]()
    override func viewDidLoad()
    {
        super.viewDidLoad()

        dbRef = FIRDatabase.database().reference()

        // Loads data to cell.
        loadData()
    }
    override func numberOfSections(in tableView: UITableView) -> Int
    {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {   
        //return the number of rows
        return places.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        // Table view cells are reused and should be dequeued using a cell identifier.

        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PlacesTableViewCell  else {
            fatalError("The dequeued cell is not an instance of PlacesTableView Cell.")
        }

        let place = places[indexPath.row]

        cell.placeLabel.text = place.name
        cell.ratingControl.rating = place.rating

        return cell

    }
    private func loadData()
    {
        dbRef!.observe(.childAdded, with: {
            (placeSnapshot) in
            //print("Adding place \(placeSnapshot.key)...")

            let labels = placeSnapshot.childSnapshot(forPath: "placeLabel")

            for (key, label) in labels.value as! [String: String] {
                self.updatePlace(key, label: label)
            }
            let ratings = placeSnapshot.childSnapshot(forPath: "rating")
            for (key, rating) in ratings.value as! [String: Int] {
                self.updatePlace(key, rating: rating)
            }
        })
 }

    private func updatePlace(_ key: String, label: String? = nil, rating: Int? = nil)
    {
        if let label = label {
            loadedLabels[key] = label

        }
        if let rating = rating {
            loadedRatings[key] = rating
        }
        guard let label = loadedLabels[key], let rating = loadedRatings[key] else {
            return
        }
        if let place = Places(name: label, rating: rating) {
            places.append(place)
            placesTableView.reloadData()
        }
    }

}

Places swift
import UIKit

class Places {

    //MARK: Properties

    var name: String
    var rating: Int

    //MARK:Types

    struct PropertyKey {
        static let name = "name"
        static let rating = "rating"
    }

    //MARK: Initialization

    init?(name: String, rating: Int) {
        // Initialize stored properties.
        self.name = name
        self.rating = rating

        // Initialization should fail if there is no name or if the rating is negative.
        // The name must not be empty
        guard !name.isEmpty else {
            return nil
        }

        // The rating must be between 0 and 5 inclusively
        guard (rating >= 0) && (rating <= 5) else {
            return nil
        }

    }

}

A:

This is a pretty verbose answer but it will iterate over the child nodes of the rating node and calculate the average
let parentRef = self.dbRef.child("Paradosiako - Panorama")

let ratingRef = parentRef.child("rating") // rating node
ratingRef.observe(.value, with: { snapshot in
    let count = snapshot.childrenCount
    var total: Double = 0.0
    for child in snapshot.children {
        let snap = child as! FIRDataSnapshot
        let val = snap.value as! Double
        total += val
    }
    let average = total/Double(count)
    print(average)
})

EDIT
To iterate over all the places and get the averages, here's the code
let placesRef = self.dbRef.child("akapnapp")

placesRef.observeSingleEvent(of: .value, with: { snapshot in

    for child in snapshot.children {

        let placeSnap = child as! FIRDataSnapshot
        let ratingsSnap = placeSnap.childSnapshot(forPath: "rating")

        let count = ratingsSnap.childrenCount
        var total: Double = 0.0
        for child in ratingsSnap.children {
            print(child)
            let snap = child as! FIRDataSnapshot
            let val = snap.value as! Double
            total += val
        }
        let average = total/Double(count)
        print(average)
    }
})

and the output showing the code works correctly. My child nodes were named a,b,c instead of the childByAutoId in the question but it works either way.
Snap (a) 3
Snap (b) 3
3.0
Snap (a) 2
Snap (b) 4
Snap (c) 5
3.67

