aggregation ?

Hi all,

I'm having an issue with aggregation and i've not been able to find a good answer.
I have a dataset that is made up off a number of collection of records. Something like

id   groupid  value   updatedOn   
1    1             xx        19/01/2021 10:00
2    1             xx        19/01/2021 10:00
3    1             xx        19/01/2021 10:00

4    2             xx        10/01/2021 10:00
5    2           xx        10/01/2021 10:00
6    2             xx        01/01/2021 10:00

7    2             xx        01/02/2021 10:00
8    3             xx        01/02/2021 10:00
9    4             xx        01/02/2021 10:00

I need to display the total responses grouped by the groupid each month, and display in a line graph.

something like
Month  GroupCount
Jan        2
Feb      1

I was thinking aggregation but im not having much luck


  Discussion posts and replies are publicly visible

  • The way I generally do this is by creating a view on the database side which breaks out the month and year into their own fields with something like MSSQL's DATEPART() function - then you have separate values to aggregate on, vs dealing with the datetime stamp in its entirety.

  • To display the total responses grouped by the groupid each month in a line graph, you will need to perform some aggregation on your dataset.

    Here are the general steps you can follow to achieve this:

    1. Group the records by the "groupid" and "month" fields. This will give you a collection of records that have the same groupid and month.

    2. Use the aggregate function (e.g. $sum, $count) to calculate the total number of responses for each groupid and month.

    3. Sort the data by the "month" field in ascending order.

    4. Plot the data on a line graph, with the "month" field on the x-axis and the total number of responses on the y-axis. Use different colors or labels for each groupid.

    For example, if you are using MongoDB, you can use the following pipeline to aggregate the data:

    db.collection.aggregate([ { $group: { _id: { groupid: "$groupid", month: "$month" }, totalResponses: { $sum: "$responses" } } }, { $sort: { _id.month: 1 } } ])

    This pipeline groups the data by "groupid" and "month", calculates the total number of responses for each group and month, and sorts the data by the month in ascending order.

    You can then take this aggregated data and use it to plot the line graph in your preferred front-end library like D3.js, Chart.js, Highcharts, etc. Please note that the above code is just an example, you may need to adapt it to your specific data structure.

    I hope this helps! Let me know if you have any other questions.

  • 0
    Certified Lead Developer

    I haven't tried this myself yet exactly, but I'd assume this is the sort of thing the newer "GroupingFunction" functionality in a!queryAggregationColumn() is for.  Based on your provided column names, I'd expect maybe something like this might just work... and if not, it'd be useful to see what your exact code is, what your exact output is, and how it fails to meet your expected output.

    a!queryAggregation(
      aggregationColumns: {
        a!queryAggregationColumn(
          field: "groupid",
          isGrouping: true()
        ),
        a!queryAggregationColumn(
          field: "updatedOn",
          isGrouping: true(),
          groupingFunction: "MONTH"
        ),
        a!queryAggregationColumn(
          field: "id",
          isGrouping: false(),
          aggregationFunction: "count",
          alias: "groupCount"
        )
      }
    )