MichelDiz
9/3/2018 - 3:10 PM

Dgraph https://discuss.dgraph.io/t/how-to-groupby-date/3040/

{
  set {

    #We create the groups as nodes
    _:group_year <name> "1979" .
    _:group_year <release_date> "1979-01-01" .
    
    _:group_year2 <name> "1950" .    
    _:group_year2 <release_date> "1950-01-01" .
    
    _:group_year3 <name> "1990" .
    _:group_year3 <release_date> "1990-01-01" .

    _:typeAlert <name> "alert" .
    _:typeAlert <type> "" .
    
    _:typeProblem <name> "problem" .
    _:typeProblem <type> "" .
    
     # Types
    _:group_month <name> "ago" .
    _:group_year3 <release_date> "1990-08-01" .

    # Then we relate them to the Nodes created 
    # (to use some existing node would just 
    # exchange the BlankNode for the UID from 
    # that existing node structure )
    # e.g: <0x1fa43> <group_year> _:NodeA .

    _:NodeA <name> "Node A" .
    _:NodeA <release_date> "1979-12-07" .
    _:group_year <group_year> _:NodeA .
    _:typeAlert <group_type> _:NodeA .

    _:NodeB <name> "Node B" .
    _:NodeB <release_date> "1979-12-07" .
    _:group_year <group_year> _:NodeB .
    _:typeAlert <group_type> _:NodeB .

    _:NodeC <name> "Node C" .
    _:NodeC <release_date> "1950-12-07" .
    _:group_year2 <group_year> _:NodeC .
    _:typeProblem <group_type> _:NodeC .

    _:NodeD <name> "Node D" .
    _:NodeD <release_date> "1990-12-07" .
    _:group_year3 <group_year> _:NodeD .
    _:typeProblem <group_type> _:NodeD .

    _:NodeE <name> "Node E" .
    _:NodeE <release_date> "1990-12-07" .
    _:group_year3 <group_year> _:NodeE .
    _:group_month <group_month> _:grou_month .
    _:typeProblem <group_type> _:NodeE .

  }
}
{
  var(func:has(release_date)) 
  @groupby(~group_year)
    @filter(ge(release_date, "1950-05-01") AND le(release_date, "1980-07-23"))
    {
      a as count(uid)
      # a is a genre UID to count value variable
    
  }

  byYear(func: uid(a), orderasc: name) {
    uid
    name
    group_year {expand(_all_)}
  }
}
#response
{
  "data": {
    "byYear": [
      {
        "uid": "0x2745",
        "name": "1950",
        "group_year": [
          {
            "name": "Node C",
            "release_date": "1950-12-07T00:00:00Z"
          }
        ]
      },
      {
        "uid": "0x2744",
        "name": "1979",
        "group_year": [
          {
            "name": "Node A",
            "release_date": "1979-12-07T00:00:00Z"
          },
          {
            "name": "Node B",
            "release_date": "1979-12-07T00:00:00Z"
          }
        ]
      }
    ]
    }
}
{
  var(func:has(release_date)) 
  @groupby(~group_year)
    @filter(ge(release_date, "1950-05-01") AND le(release_date, "1980-07-23"))
    {
      a as count(uid)
      # a is a genre UID to count value variable
    
  }

  byYear(func: uid(a), orderasc: name) {
    uid
    name
    group_year @filter(ge(release_date, "1950-05-01") AND le(release_date, "1979-07-23"))
      {expand(_all_)}
  }
}
{
  "data": {
    "byYear": [
      {
        "uid": "0x2745",
        "name": "1950",
        "group_year": [
          {
            "name": "Node C",
            "release_date": "1950-12-07T00:00:00Z"
          }
        ]
      },
      {
        "uid": "0x2744",
        "name": "1979"
      }
    ]
  }
}