Need help with dgraph query

As far I can tell this wouldn’t be possible. Cuz they are multiple orders. We can count, but not “count unique”. However we can do this if we define a new block for each Customer. And that would have to be done manually. As you did in your first example.

Using the new @normalize behavior (coming soon), we can do something close to what you want. But it doesn’t take out the repeated ones.

{
  q(func:  type(Customer)) {
   customer_name : name
    did_order @normalize {
      order.item  {
       count : count(uid)
       item.name : item.name
      }
    }
  }
}

It returns

{
  "data": {
    "q": [
      {
        "customer_name": "Alice",
        "did_order": [
          {
            "item.name": "pencil"
          },
          {
            "item.name": "quill"
          },
          {
            "count": 2
          },
          {
            "item.name": "pen"
          },
          {
            "item.name": "pencil"
          },
          {
            "count": 2
          }
        ]
      },
      {
        "customer_name": "bob",
        "did_order": [
          {
            "item.name": "quill"
          },
          {
            "count": 1
          },
          {
            "item.name": "quill"
          },
          {
            "count": 1
          }
        ]
  }
}

You can just “remove” the repeated ones manually.

@animesh2049 what you think about this? We could add a new feature like “@mergenormalized

{
  q(func:  type(Customer)) {
   customer_name : name
    did_order @mergenormalized(item.name) {      # "Merge by name"
      order.item  {
       count : count(uid)
       item.name : item.name
      }
    }
  }
}

Tha would return

{
  "data": {
    "q": [
      {
        "customer_name": "Alice",
        "did_order": [
          {
            "item.name": "pencil"
          },
          {
            "item.name": "quill"
          }
          {
            "item.name": "pen"
          },
          {
            "count": 3
          }
        ]
      },
      {
        "customer_name": "bob",
        "did_order": [
          {
            "item.name": "quill"
          }
          {
            "count": 1
          }
        ]
      }
    ]
  }
}