Accounts listing aggregation issues

Changed

I have a view that lists accounts within financial entities, and want to show the most recent statement using views aggregation.

I'm running in to a handful of issues with this.

To get started, add a date field to Article and use Devel Generate for some dummy content.

Then create a view that lists article titles in a table, with no sort.

Here is the query that is generated.

SELECT 
  node_field_data.nid 
    AS nid 
FROM 
  node_field_data node_field_data 
WHERE 
  node_field_data.type 
    IN ('article')

Now enable aggregation.

SELECT 
  MIN(node_field_data.nid) 
    AS nid 
FROM 
  node_field_data node_field_data 
WHERE 
  node_field_data.type 
    IN ('article')

With the nid now in MIN(), the lowest nid content is the only item shown.

Views aggregated field settings
  • View
  • These are the default aggregation settings for the title field.

Grouping by entity ID seems to mean "get the lowest nid content".

The other option is to group by value, which will list all the titles.

SELECT 
  node_field_data.title 
    AS node_field_data_title, 
  MIN(node_field_data.nid) 
    AS nid 
FROM 
  node_field_data node_field_data 
WHERE 
  node_field_data.type 
    IN ('article') 
GROUP BY 
  node_field_data_title

This adds the title value to the select list and group by, so keep it on entity ID.

Here's where things get tricky. Add the date field, and use maximum for the aggregation type.

Now there's a lot of extra things in the query.

SELECT 
  MAX(
    node__field_date.field_date_value
  ) 
    AS node__field_date_field_date_value, 
  MAX(node__field_date.delta) 
    AS node__field_date_delta, 
  MAX(node__field_date.langcode) 
    AS node__field_date_langcode, 
  MAX(node__field_date.bundle) 
    AS node__field_date_bundle, 
  MIN(node_field_data.nid) 
    AS nid 
FROM 
  node_field_data node_field_data 
  LEFT JOIN node__field_date node__field_date 
    ON node_field_data.nid = node__field_date.entity_id 
  AND node__field_date.deleted = '0' Views Aggregator Plus module
WHERE 
  node_field_data.type 
    IN ('article')

Issues

Views Aggregator Plus module

Configured a view with fields for the content type (set to group and compress in table aggregation), integer (set to max), title (display first, hidden), and a custom text field.

Sort by the date field, descending.

Use the hidden title token in the custom text to print the title with a link. Otherwise, the aggregation will replace it.

There's a bug related to this: View on Drupal.org #3279959Enumerate (Sort, no dupl.)* prints html, does not render html Status: Active

And a bug that fixes the module (was broken otherwise): View on Drupal.org #3482129Crashes with Drupal\views_aggregator\Plugin\views\style\Table::setAggregatedGroupValues(): Argument #3 ($group_aggregation_results) must be of type int, string given Status: Reviewed & tested by the community

The SQL query is just sorting by date, and the display filters the results for the max.

SELECT 
  node__field_integer.field_integer_value 
    AS node__field_integer_field_integer_value, 
  node_field_data.nid 
    AS nid 
FROM 
  node_field_data node_field_data 
  LEFT JOIN node__field_integer node__field_integer 
    ON node_field_data.nid = node__field_integer.entity_id 
  AND node__field_integer.deleted = '0' 
WHERE 
  (node_field_data.status = '1') 
  AND (
    node_field_data.type 
    IN ('article')
  ) 
ORDER BY 
  node__field_integer_field_integer_value DESC

Other modules

Views Raw SQL
View Views Raw SQL on Drupal.orgViews Raw SQL

Views Timestamp Aggregate
View Views Timestamp Aggregate on Drupal.orgViews Timestamp Aggregate

Views Argument Substitutions (dev only)
View Views Argument Substitutions on Drupal.orgViews Argument Substitutions

Views Combine
View Views Combine on Drupal.orgViews Combine

Views Field View
View Views Field View on Drupal.orgViews Field View

Views Filters Extras
View Views Filters Extras on Drupal.orgViews Filters Extras

ECA (ECA Views submodule)
View ECA on Drupal.orgECA

Number Formatter
View Number Formatter on Drupal.orgNumber Formatter
#2849881: View on Drupal.org #2849881Field formatters no longer work for aggregated fields in Views Status: Active

Notes

https://www.chapterthree.com/blog/using-hook-views-query-alter

https://www.lullabot.com/articles/building-views-query-plugins-dupal-8-part-1

https://www.hashbangcode.com/article/drupal-8-creating-subquery-views

https://drupal.stackexchange.com/questions/288422/join-subquery-as-right-table-in-views

https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Plugin%21views%21query%21Sql.php/property/Sql%3A%3Ahaving/11.x

https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Plugin%21views%21join%21Subquery.php/class/Subquery/11.x

https://www.drupal.org/node/2770421

https://www.drupal.org/docs/8/api/database-api/dynamic-queries/conditions#s-using-subqueries-in-conditions

https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Plugin%21views%21join%21JoinPluginBase.php/group/views_join_handlers/11.x

https://www.drupal.org/docs/8/api/database-api/dynamic-queries/joins

https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/comment/src/Plugin/views/sort/StatisticsLastCommentName.php?ref_type=heads

https://ecaguide.org/plugins/eca/views/actions/eca_views_query_substitution/

https://www.freelock.com/blog/john-locke/2020-04/aggregate-fields-drupal-8-views

Task type
Project
Modules