skip to Main Content

I want to get the total number count until specified.

this is my database: database picture

I want to count where ticket_status is not served

ex: I want to count until A6, I will count A1 until A5 where the value of ticket_status" is not served. so the final count will be 3.

is there any filtering kind?

I have this code but it just counts all nodes with the value of "not served"

dbref.child("Queue").child(a.value.toString()).orderByChild("ticket_status").equalTo("not served").addValueEventListener(object : ValueEventListener{
                                override fun onDataChange(snap: DataSnapshot) {
                                        val count = snap.childrenCount
                              dbref.child("Student_Queue_Reserve").child(idNumber.toString()).child("people_ahead").setValue((count-1).toString())
                                }
                                override fun onCancelled(error: DatabaseError) {
                                }
                            })

2

Answers


  1. I want to count until A6, I will count A1 until A5 where the value of ticket_status" is not served. So the final count will be 3.

    There is no way you can do that in the Realtime Database because queries can only order/filter on a single property. There are cases in which you can combine the values you want to filter into a single property. For example, please check my answer below:

    If you’re allowed to change the database schema, then you should consider denormalizing the data. So a structure like below will do the trick:

    db
    |
    --- Queue
         |
         --- Accounting Office
               |
               --- served
               |    |
               |    --- A1
               |    |   |
               |    |   --- serving_token: "A001"
               |    |   |
               |    |   --- ticket_status: "served"
               |    |
               |    --- A2
               |        |
               |        --- serving_token: "A002"
               |        |
               |        --- ticket_status: "served"
               |
               --- not served
                    |
                    --- A3
                    |   |
                    |   --- serving_token: "A003"
                    |   |
                    |   --- ticket_status: "not served"
                    |
                    --- A4
                    |   |
                    |   --- serving_token: "A004"
                    |   |
                    |   --- ticket_status: "not served"
                    |
                    --- A5
                    |   |
                    |   --- serving_token: "A005"
                    |   |
                    |   --- ticket_status: "not served"
                    |
                    --- A6
                        |
                        --- serving_token: "A006"
                        |
                        --- ticket_status: "not served"
    

    Now you can query the "not served" node, using a combination of Query#orderByKey() and Query#endAt(java.lang.String)

    db.child("Queue").child("Accounting Office").child("not served").orderByKey().endAt("A6");
    
    Login or Signup to reply.
  2. You can try this approach without query:

    ArrayList servedArray ;
    ArrayList notServedArray ;
    
    • get a data snapshot
    • loop through snapshot
    • if value == "served" >> add to servedArray
    • else if value equal == "not served" add to notServedArray

    And for count you can use :

    servedArray.size
    notServedArray.size
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search