Unique count for each node of the same type

Hello.

I’m trying to run this query: Which Employee had the Highest Cross-Selling Count of ‘Chocolade’ and Another Product? in Dgraph with DQL.

To do so I need to:

  • Find all the employees that have sold chocolade
  • Find the orders and products (orders—>products) of employees that have sold chocolade
  • Count the products.

The problem I’m facing is that given Order_A {Product_A, Product_B} and Order_B {Product_A, Product_C}, I can’t get 3 as the result when I use the count method. Instead of 3, I get Order_A: 2; and Order_B: 2.

I know that each Order has 2 products. But is there a way of counting the total product, without duplication? For example:
Product_A, Product_A = 1
Product_B = 1
Product_C = 1

Total = 3

I have tried to do this with GraphQL, but now I’m trying to this with DQL (I can use variables there). My current query is:

{
	var(func: eq(Product.ProductName, "Chocolade")) {
		Product.orders {
		  Order.employee {
			employeesOrder as Employee.FirstName
          }
        }
    }

  totalOrders(func: type(Order)) @cascade{
		Order.employee @filter(uid(employeesOrder)){
			Employee.FirstName
            Employee.EmployeeID
        }
        count(Order.products)
  }
}

Here is part of the schema (in graphQL)

type Order {
  OrderID: Int! @id @search
  OrderDate: DateTime
  RequiredDate: DateTime
  ShippedDate: DateTime
  Freight: Float
  ShipName: String @search(by: [exact])
  ShipAddress: String
  ShipCity: String
  ShipRegion: String
  ShipPostalCode: String
  ShipCountry: String
  employee: Employee
  products: [Product]
  shipper: Shipper @hasInverse(field: orders)
  customer: Customer
}

type Product {
  ProductID: Int! @id @search
  ProductName: String! @search(by: [exact])
  QuantityPerUnit: String
  UnitPrice: Float
  UnitsInStock: Int
  UnitsOnOrder: Int
  ReorderLevel: Int
  Discontinued: Int!
  supplier: Supplier @hasInverse(field: products)
  orders: [Order] @hasInverse(field: products)
  category: Category
}

type Employee {
  EmployeeID: Int! @id @search
  FirstName: String!
  LastName: String!
  Title: String
  TitleOfCourtesy: String
  BirthDate: DateTime
  HireDate: DateTime
  Address: String
  City: String
  Region: String
  PostalCode: String
  Country: String
  HomePhone: String
  Extension: String
  Notes: String
  PhotoPath: String
  manager: Employee @hasInverse(field: employees)
  employees: [Employee]
  territories: [Territory] @hasInverse(field: employees)
  orders: [Order] @hasInverse(field: employee)
}

Does anyone have any tips?