It would be very useful for to have a GROUP_CONCAT available as an aggregate function. like what we have now e.g. SUM, MIN, ...

CommentFileSizeAuthor
#33 add_group_concat_aggregate-2902481-33.patch12.96 KBnavkaur
#31 2902481-31.patch12.97 KBKapilV
#30 add_group_concat_aggregate-2902481-30.patch12.87 KBjackson.cooper
#29 interdiff_28_29.txt5.42 KBKapilV
#29 2902481-29.patch11.82 KBKapilV
#28 add_group_concat_aggregate-2902481-18.patch6.95 KBjackson.cooper
#17 add_group_concat_aggregate-2902481-17.patch6.47 KBattisan
#15 add_group_concat_aggregate-2902481-13.patch728 bytesSimeonKesmev
#12 add_group_concat_aggregate-2902481-12.patch725 bytesachton
#11 add_group_concat_aggregate-2902481-11.patch761 byteskasperg
#10 add_group_concat_aggregate-2902481-9.patch1.07 KBkasperg
#8 add_group_concat_aggregate-2902481-8.patch751 bytesSir-Arturio
#3 add-group-concat-aggregate-2902481-1.patch1.1 KBb.ravanbakhsh
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

b.ravanbakhsh created an issue. See original summary.

david_garcia’s picture

GROUP_CONCAT is not part of the SQL ANSI standard so it will limit portability of the feature.

This should be dealt with in contrib.

b.ravanbakhsh’s picture

Here's the patch.

Works for me.

Try it with a view of nodes grouped on uid and GROUP_CONCAT on nid or title.

joachim’s picture

IIRC there is already an issue for this.

b.ravanbakhsh’s picture

@david_garcia have not seen your comment before post patch.
As I know PostgreSQL does not have group_concat but has "array_agg" so we can not fix that.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Sir-Arturio’s picture

The patch fails for me (for 8.4.x). I think it's missing ']' character.

Also I'd like to point out that group_concat is used for other fields than just numeric. Changing 'field' => 'numeric' to 'field' => 'field' enables concatenation of textual fields as well.

There are already some issues (like the one I linked as related) but no real fixes. Some recommend using the contrib module Views Aggregator Plus but does not solve the problem for me. It introduces a whole new paradigm for views which has it's own problems.

Sir-Arturio’s picture

Here is my crude patch of how I got the group concat to work.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

kasperg’s picture

The patch does not apply using patch level p2. Here is an updated one that does.

The change might be crude, not portable and lacking features such as the ability to define separators and the like, but for a simple case it has worked well for me.

kasperg’s picture

FileSize
761 bytes

Patch in #10 does not apply either. This should work.

achton’s picture

Here is the patch from #11 rerolled for Drupal 8.6.1.

m.lebedev’s picture

It works. thanks

Pancho’s picture

Version: 8.6.x-dev » 8.8.x-dev
Assigned: b.ravanbakhsh » Unassigned

New features go into the latest dev branch. Also unassigned after 18 months.

SimeonKesmev’s picture

The field argument for the handler shall not be "field" as it tries to load it from the entity object. I am having problem when trying to actually output it.
Here is an updated patch.

gambry’s picture

Issue tags: +Needs tests

This needs tests.

attisan’s picture

tested #15 patch and found

  • that it would prevent saving views actually using group_concat (on strings)
  • hence added a GroupByString to views and
  • added Group Concat DISTINCT option
Beakerboy’s picture

Wouldn’t the addition of GROUP_CONCAT to Core limit it’s installation on non MySQL databases?

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

brooke_heaton’s picture

Patch #17 is working well for me. I would however love to see some more settings on this Filter Plugin - specifically to allow the substitution of a separator (e.g. not a comma) also there is no space between records. Allowing a unique separator would fix that. I'm wondering why this does not allow the use of token/twig replacements. Maybe that's too much to task.

codesmith’s picture

Patch in #17 works great for me - thanks!

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mellowtothemax’s picture

#17 works for me too, but it only returns ID values. The formatting options are removed from the field.

This would be great to add as well as what #20 suggested.

This works with data export which is a bonus as its needed for xml feeds for commerce.

Correction. The ID value only returned on commerce variation attributes. With the correct relationship fields it works correctly.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

petednz’s picture

Patch has solved our problems too. thank you. we used "rewrite" and {{ |replace({',': "
"})|raw}} to get each value on to its own line. assuming same could be done if someone wanted to replace 'comma' with 'comma space'

petednz’s picture

Status: Needs review » Reviewed & tested by the community
daffie’s picture

@petednz: We need to add testing for the added functionality. First to test if the added functionality does what it is supposed to and second to make sure that it keep working and that some future update does not break the added functionality.

jackson.cooper’s picture

Here's patch #17 that fixes the PHP 7.4 deprecation notice and some linting errors.

KapilV’s picture

Status: Needs work » Needs review
FileSize
11.82 KB
5.42 KB

Fixed PHPCss error.

jackson.cooper’s picture

The original patch was missing some code. I've added it back and included the changes in #29.

KapilV’s picture

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

navkaur’s picture

The above patch wasn't applying to core version 9.2.2 and above.
Its minor change which removes the space from the comment line // https://www.drupal.org/node/571548. to make it compatible with latest version.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Adam Clarey’s picture

The latest patch doesn't work for postgresql

Adam Clarey’s picture

Just testing locally this works:

public function aggregationMethodConcatSimple($group_type, $field) {
    return strtoupper($group_type) . '(' . $field . ', \',\')';
  }

It needs to specify the string to use as glue, ie ','

smustgrave’s picture

Status: Needs review » Needs work

Moving back to needs work based on comment #36 and no test cases being added

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Andrew Answer’s picture

Can we have a list of entity links instead of list of strings? It would be very useful too.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.