Count unique item in Child table

Hi there,

I have a child table that contains (Items), and now I want to count unique items in the child table
In the following picture,

I use the following code to create sales orders,

docu = frappe.new_doc('Sales Order')
docu.customer = 'Osama'
docu.order_type = "Sales"

for x in doc.medication_orders:
    docu.append("items", {
        'item_code' : x.drug,
        'delivery_date': doc.start_date,
        'qty': 1,
        'uom': doc.dosage_form

this code creates a Sales Order with the same Child table in Inpatient Medication Order,
the final result should be like this

Name Qty
Omega 2
Alarmin 2

@Osama_Muhammad You can do that with ease using Query Builder.

Replace "Sales Order Name" with the name of row you want, like

from frappe.query_builder.functions import Count

doc = frappe.qb.DocType("Sales Order Item")
data = (
        .select(doc.item_code, Count(doc.item_code).as_("total"))
        .where(doc.parent.eq("Sales Order Name"))
        .where(doc.parenttype.eq("Sales Order"))

The result will be a list of dict with keys (item_code, total)