Remove Duplicate Views Results

drupal remove duplicate views results

When you are building a Drupal site and creating views, it’s not uncommon to find that your view is showing the same result multiple times.

Here are two common ways to remove duplicate views results.

Option 1: Distinct

This is how Views describes the Distinct option:

“If there are multiple identical items, each will be displayed only once. You can use this to try and remove duplicates from a view, though it does not always work.”

  • Open the Advanced area.
  • Next to “Query settings”, click “Settings”.
media_1389388765421.png
  • Check the boxes, “Distinct” and “Pure Distinct”.
media_1389388828163.png

Option 2: Aggregation

Aggregration is often used to perform calculations on field data. For example, you can use Aggregation to count the number of content items in a particular content type. Or you could use it count the average number of comments on your content.

However, if the Distinct option doesn’t work, we can use also try to use aggregation to remove duplicate items.

  • Open the Advanced area.
  • Next to “Use aggregation”, click “No”.
media_1389534990668.png
  • Check the “Aggregate” box.
media_1389535015509.png
  • On the left-hand side of the view, notice that “Aggregation settings” is now an option in several areas.
  • Click “Aggregation settings” for your Filter Criteria.
media_1389536296139.png
  • Choose “Count DISTINCT”.
media_1389536331505.png
  • Click on the name of your Filter Criteria.
  • Choose “is equal to” and set “Value” to “1”.
media_1389536414529.png

Instructor

  • Steve is the founder of OSTraining. Originally from the UK, he now lives in Sarasota in the USA. Steve's work straddles the line between teaching and web development.

0 0 votes
Blog Rating
Subscribe
Notify of
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
pepperstreet

Thanks for the article!

PS: I guess “Pure Distinct” parameter needs a real world example. At least my designer-brain needs one 😉

Alex Weber

Good tips, but it’s worth stressing that DISTINCT has an impact on query performance so if you choose to go down any of these paths, you might want to consider using some Views caching to mitigate the impact.
Ideally we’d be able to add a GROUP BY using Views but it’s built in such a way that it’s hard to group by just single id field… I’ve had good results hook_views_query_alter()ing Views and manually adding that group by n.nid for example too.
Edit: actually, views_query_alter() won’t work, it’s gotta be straight up hook_query_alter()

steve

Thanks Alex!

RdeBoer

Good article and helpful comment by Alex Weber.
[plug]

This article introduces View’s aggregation options.

If the standard Views functionality does not go far enough, try [url=http://drupal.org/project/views_aggregator]Views Aggregator Plus[/url].

Renaud CUNY

Nice article, and agree on both comments. You can also give a try to [url=https://drupal.org/project/views_distinct]https://drupal.org/project/…[/url] which is not perfect but can handle tricky situations.

steve

Thanks RdeBoer and Renaud
I think the key takeaway from this article and the comments that there’s multiple causes of duplicate results and therefore, there’s multiple solutions also.

Gilberto Mangones

Excelente this details

danielpickering

Thanks Gilberto

Ravi Lamontagne

The second alternative worked great for my duplication issue. Thanks.

Kylie

I am using the “use Aggregation” for distinct value . but in my field having image value its not working with images .If i remove Image field its working fine .Please suggestion how can use with image field ?

10
0
Would love your thoughts, please comment.x
()
x