I know that this question turn around in Drupal answers but I couldn't get a working answer for my use case.
Here it is: I have a view which is producing duplicate results (and I understand why) but I couldn't find a way to remove them in the hook views_query_alter
This view is requesting nodes with some filters and a left join (Require this relationship) on a field wich contains references to paragraphs (node__field_aw_agenda
). This is this relationship which produce the duplicates.
SELECT "node_field_data"."nid" AS "nid", "node_field_data"."langcode" AS "node_field_data_langcode", DATE_FORMAT(paragraphs_item_field_data_node__field_aw_agenda__paragraph__field_agenda_date.field_agenda_date_value, '%Y%m%d') AS "paragraphs_item_field_data_node__field_aw_agenda__paragraph_", MIN(node_field_data.nid) AS "nid_2", MIN(paragraphs_item_field_data_node__field_aw_agenda.id) AS "paragraphs_item_field_data_node__field_aw_agenda_id"
FROM
{node_field_data} "node_field_data"
LEFT JOIN {node__field_aw_agenda} "node__field_aw_agenda" ON node_field_data.nid = node__field_aw_agenda.entity_id AND node__field_aw_agenda.deleted = '0'
INNER JOIN {paragraphs_item_field_data} "paragraphs_item_field_data_node__field_aw_agenda" ON node__field_aw_agenda.field_aw_agenda_target_id = paragraphs_item_field_data_node__field_aw_agenda.id
LEFT JOIN {node__field_aw_prod_state} "node__field_aw_prod_state" ON node_field_data.nid = node__field_aw_prod_state.entity_id AND node__field_aw_prod_state.deleted = '0'
LEFT JOIN {node__field_aw_images} "node__field_aw_images" ON node_field_data.nid = node__field_aw_images.entity_id AND node__field_aw_images.deleted = '0'
LEFT JOIN {paragraph__field_agenda_date} "paragraphs_item_field_data_node__field_aw_agenda__paragraph__field_agenda_date" ON paragraphs_item_field_data_node__field_aw_agenda.id = paragraphs_item_field_data_node__field_aw_agenda__paragraph__field_agenda_date.entity_id AND paragraphs_item_field_data_node__field_aw_agenda__paragraph__field_agenda_date.deleted = '0'
WHERE ("node_field_data"."status" = '1') AND ("node_field_data"."type" IN ('aw_audiovisu', 'aw_scenicarts')) AND ("node__field_aw_prod_state"."field_aw_prod_state_value" IN ('prem', 'play')) AND ("node_field_data"."langcode" IN ('fr')) AND ("node__field_aw_images"."field_aw_images_target_id" IS NOT NULL)
GROUP BY "node_field_data"."nid", "paragraphs_item_field_data_node__field_aw_agenda__paragraph_", "node_field_data_langcode"
ORDER BY "paragraphs_item_field_data_node__field_aw_agenda__paragraph_" DESC
As you can see, there is a GROUP BY
term with several fields in it. This GROUP BY
term is added in the following hook
function mymodule_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
$view_id = $view->id();
$display_id = $view->current_display;
if (($view_id == 'my_view') && ($display_id == 'my_display_id')) {
$query->addField('node_field_data', 'nid', 'nid', [
'function' => 'groupby',
]);
$query->groupby = ['node_field_data.nid'];
}
}
Note that this hook is adding only one GROUP BY
field ("node_field_data"."nid"
), I don't know how this lead to add the other fields ("paragraphs_item_field_data_node__field_aw_agenda__paragraph_", "node_field_data_langcode"
)
Please do not provide me a solution with hook_views_pre_render()
as I am looking for a generic SQL solution
As requested, here is a screenshot of the view UI