I'm getting duplicate taxonomy terms in my view, even though I have checked the 'Reduce duplicates' box...
Is this a bug, or does it just not work properly yet?

CommentFileSizeAuthor
#18 view.export.txt32.49 KBvthirteen
#4 View.txt4.73 KBaleagi
#3 ViewExport.txt17.42 KBAnonymous (not verified)

Comments

Anonymous’s picture

Sorry, I should rephrase:
I'm getting duplicate nodes in my view due to some nodes having more-than-one taxonomy term.

Example:

278  28/11/2008  Jetstar             28/11/2008  Cash   $0.00
279  01/12/2008  Qantas              02/02/2009  Other  $0.00  Comments go here...
279  01/12/2008  Singapore Airlines  02/02/2009  Other  $0.00  Comments go here...

In this case, the different airlines are taxonomy terms.

merlinofchaos’s picture

I can't help without an export of the view, as I have no idea how it's configured.

Anonymous’s picture

StatusFileSize
new17.42 KB

Right, here you go then...
(Thought I'd attach it, rather than paste it here)

Oh, and by the way, the example I posted above doesn't match up with the fields in the export as I deleted some values that weren't necessary.

aleagi’s picture

StatusFileSize
new4.73 KB

Hi,

I'm having the same problem here. In fact I was studying the problem and have customized a simple view to better presentation of the problem.

In the attached view there's only the NID (to be sure that the node is repeated), the Node Title and the Taxonomy Term Field.

Yes, I've also checked the Distinct on the Basic Configuration. The problem is that the view is, even with the checked box, showing duplicated nodes.

Nothing wrong until now...

The problem, in my point of view, is located on the Distinct, that is not distincting nothing at all. I mean, even with the distinct function enabled, the nodes are still being duplicated, one for each taxonomy term.

Is it possible to have, again, the Node: Distinct Filte?!? It seems to work better than the present one. Or, if needed, to add the filter AND the distinc on basic configuration block, just to guarantee that the node will be showed only once.

I mean, a redundant option (not so redundant as the first option are not working as we expected) like a: Reduce multi-terms repetitions or something.

Thanks in advance, Any help will be appreciated!

Anonymous’s picture

Any updates on this? Solutions?

merlinofchaos’s picture

Status: Active » Fixed

You should be using the "Taxonomy: All terms" field if you can have multiple terms on a node. In the field options you can restrict which vocabulary you want the terms to appear from.

Anonymous’s picture

Yay! Thanks Merlin!

superhua’s picture

Thank you!

Is there anyway to display the multiple _descriptions_ of the taxonomy terms without causing duplicates?

That is, is there anything like a "Taxonomy: All terms' descriptions" field?

merlinofchaos’s picture

Unfortunately there is not.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

lizbethalml’s picture

Hello merlinofchaos! first of all thank you so much!!! this is a great module!!!!!!! that is a good solution... the one of taxonomy all terms but what I want to displan is the taxonomy image! so is there a way to do this from here? cause I don't want to cause duplicates... thank's again!

bloke_zero’s picture

If you can't restrict yourself to one vocabulary for any reason (say you need more than 1!) you can still get there by using the node view and theming the node (in my case a product) and having different displays depending on whether it was a teaser or full node view and print a few of the fields in the teaser view rather than the whole lot.

I found using the fields display in the view useless as it allowed duplicates to appear, but using the node view (with 'Distinct' set to yes) and the theming worked.

And yes, thank you for a wonderful module! Drupal would be infinitely poorer without it.

patchak’s picture

Version: 6.x-2.1 » 6.x-2.2
Status: Closed (fixed) » Active

I have the same problem using views 6.x-2.2

I'm not using "fields" I'm only using the "node" style to make a view that replicates the core "taxonomy/term" view. I still have duplicate nodes each time I have nodes that are tagged in several categories. How can I fix this if I don't use fields as the type?

I tried setting on the "Distinct" setting, but it does not seem to have had any effect...Any ideas?

thanks a lot
Patchak

borfast’s picture

Version: 6.x-2.2 » 6.x-2.5
Component: exposed filters » taxonomy data

I am also having the same problem of having a node instance per each taxonomy term associated with it.

I have the distinct option enabled and have even cleared everything on the view except for the node title and taxonomy term fields - I still get duplicates (not that I thought that clearing everything would help in not getting duplicates; I just wanted to get a simpler SQL query to look at).

My use case is that I am using the Taxonomy Image module, which provides a "term image" field that causes exactly the same problem as the "taxonomy term" field - one node instance per each term.

The resulting query is quite simple:

SELECT DISTINCT(node.nid) AS nid,
   node.title AS node_title,
   term_data.name AS term_data_name,
   term_data.vid AS term_data_vid,
   term_data.tid AS term_data_tid
FROM node node 
LEFT JOIN term_node term_node ON node.vid = term_node.vid
LEFT JOIN term_data term_data ON term_node.tid = term_data.tid

I'm a bit rusty with SQL but I checked it and confirmed that this does indeed produce multiple rows for the same node - and we need this, otherwise we couldn't get the various taxonomy terms associated with the node.

So, as I said, I'm a bit rusty on SQL but I'm assuming the SQL code and its result set is correct, which leaves us with the problem being somewhere in the Views code that handles and displays the results.

My guess (I grepped and searched through the code but I know next to nothing of the internal workings of Views, so I may have missed it) is that Views is only using the Distinct option to generate the SQL code but in cases such as the one I presented above, it does nothing to merge the multiple results.

If that's the case, what would be the best way to fix this?

borfast’s picture

Component: taxonomy data » Views Data
dawehner’s picture

Status: Active » Fixed

Did you really read this issue, there is the solution :)

http://drupal.org/node/345571#comment-1188223

borfast’s picture

dereine, that's not quite the solution, since I was already doing that and still get duplicate terms. Perhaps I didn't explain myself well but the problem is that the result set contains several nodes with the same id (effectively, the same node) even if I limit the terms to one vocabulary.
The node type I'm selecting doesn't even have any other vocabulary associated with it.

In other words, the resulting list shows the same node several times - one entry for each term associated with it - even if I restrict the vocabulary selection and/or check the "reduce duplicates" option.

vthirteen’s picture

Title: Duplicate taxonomy terms » Taxonomy filter and taxonomy field: duplicate results if nodes have multiple taxonomy terms
Version: 6.x-2.5 » 6.x-2.6
StatusFileSize
new32.49 KB

i guess i have the same problem.

i'm filtering by node type, these nodes are tagged with single terms from two different vocabularies. incidentally, i have a "taxonomy: ID term with depth" exposed filter with "reduce duplicates" checked, plus the same taxonomy is also used as an argument (i need both ways of "filtering" content).

then i have a "Taxonomy: all terms" field with terms limited to just one vocabulary.

still, i have duplicate nodes in the views listing, that is nodes tagged with terms from more than one vocabulary appear twice.

the export of my view is attached.

vthirteen’s picture

Status: Fixed » Active

[set it to active again even though i'm not sure if in my case it's not just a matter of wrong configuration]

vlooivlerke’s picture

I have the same problem.

Multiple tags, cause multi node displays.

giorgosk’s picture

NOTE:
"taxonomy term" fields will cause duplicates of nodes that have multiple terms selected
use "taxonomy all terms" instead and only check the desired vocabulary to display
works as above with no duplicates and no need for filtering
but field is not sortable :-( ( << does anybody know of a workaround ?)

also NOTE: relationships on taxonomy terms will cause duplicates of nodes that have multiple terms selected
(delete these relationships if you don't really need them)

merlinofchaos’s picture

Status: Active » Closed (won't fix)

Reduce duplicates will not remove duplicates due to having multiple terms on a node.

If you have multiple terms on a node, there is very little you can do to show data about that term without ending up with duplicates. If you need multiple taxonomy images, then taxonomy image will need to provide a field similar to taxonomy: All terms to show the images.

In fact, anything you do that attempts to add a particular field (or sort or filter) from a term is going to cause these duplicates. These are not bugs that is the natural behavior of SQL.

dzieyzone’s picture

I was also having the same problem and to found out that it is indeed how the views was created:

The views created:

SELECT DISTINCT(node.nid) AS nid,
   node.title AS node_title,
   node_data_field_image.field_image_fid AS node_data_field_image_field_image_fid,
   node_data_field_image.field_image_list AS node_data_field_image_field_image_list,
   node_data_field_image.field_image_data AS node_data_field_image_field_image_data,
   node.type AS node_type,
   node.vid AS node_vid,
   uc_products.sell_price AS uc_products_sell_price,
   term_node.weight_in_tid AS term_node_weight_in_tid
FROM drup_node node
LEFT JOIN drup_content_field_image node_data_field_image ON node.vid = node_data_field_image.vid
LEFT JOIN drup_uc_products uc_products ON node.vid = uc_products.vid
LEFT JOIN drup_term_node term_node ON node.vid = term_node.vid
WHERE (node.status <> 0 OR node.uid = ***CURRENT_USER*** or ***ADMINISTER_NODES*** = 1) AND (node.vid IN (
  SELECT tn.vid FROM drup_term_node tn
  WHERE tn.tid  = 1
  ))
   ORDER BY term_node_weight_in_tid DESC

The DISTINCT(node.nid) is really useless because of the term_node.weight_in_tid) AS term_node_weight_in_tid therefore when I re-write the views query into

SELECT DISTINCT(node.nid) AS nid,
   node.title AS node_title,
   node_data_field_image.field_image_fid AS node_data_field_image_field_image_fid,
   node_data_field_image.field_image_list AS node_data_field_image_field_image_list,
   node_data_field_image.field_image_data AS node_data_field_image_field_image_data,
   node.type AS node_type,
   node.vid AS node_vid,
   uc_products.sell_price AS uc_products_sell_price
FROM drup_node node
LEFT JOIN drup_content_field_image node_data_field_image ON node.vid = node_data_field_image.vid
LEFT JOIN drup_uc_products uc_products ON node.vid = uc_products.vid
LEFT JOIN drup_term_node term_node ON node.vid = term_node.vid
WHERE (node.status <> 0 OR node.uid = ***CURRENT_USER*** or ***ADMINISTER_NODES*** = 1) AND (node.vid IN (
  SELECT tn.vid FROM drup_term_node tn
  WHERE tn.tid  = 1
  ))
   ORDER BY term_node.weight_in_tid DESC

By removing the term_node.weight_in_tid AS term_node_weight_in_tid in display and changing the ORDER BY term_node_weight_in_tid DESC to ORDER BY term_node.weight_in_tid DESC I was able to get a unique node items.

This means that adding additional field in the select ruins the query.

The fix I can think of is that the views should not include a field for those that are in sorted or filter options.

merlinofchaos’s picture

Unfortunately, Postgres (and thus ANSI SQL) requires that any item in the ORDER BY also be in the fields.

shashi_lo’s picture

I only need the Taxonomy title and nothing else, but here's the code I put in before the foreach

$rows = array_unique($rows);
dvness’s picture

Status: Closed (won't fix) » Active

any clues on how to get this to work properly? i'm having the same issue - all terms associated with a node are displayed, but the node appears as many times as there are terms associated with it.

dawehner’s picture

Status: Active » Closed (won't fix)

See http://drupal.org/node/345571#comment-2254238

Please take care that you have taxonomy: all terms instead of taxonomy:term as field.

weseze’s picture

I was having a similar problem.
Setup a view of nodes with 2 Taxonomy:term filters for 2 different vocabularies. Nodes with more then one term showed up more then once. With the combination of 2 vocabularies some nodes showed up 4 times and more... Simply selecting the distinct option solved my problem.

youssefr’s picture

.......#6

Hi
How do you get to this option?

Thanks.

eric constantinides’s picture

I was just having this issue too. Using "distinct" had previously not worked either. I ended up installing the "Views related nodes" module and using the "reduce duplicates" within the "Taxonomy: Term ID" argument. And voila! Problem fixed.

zoo33’s picture

Just a clarification or reiteration of what merlin says here: http://drupal.org/node/345571#comment-2544996

If you use taxonomy terms as a sort criterion and any of the matched nodes have more than one taxonomy term, you will end up with duplicates. Doesn't matter if you have a visible taxonomy field or not.

WildKitten’s picture

@ shashi_lo : Thanks for this! It did trick for me :)

I had multiple terms from same vocabulary in one node.
In view I had this:
Filds: Node title.
Filters: Tahonomy Term (exposed).

So I made views-view-unformatted.tpl.php file for my view. And add your code in right place.

<?php
// $Id: views-view-unformatted.tpl.php,v 1.6 2008/10/01 20:52:11 merlinofchaos Exp $
/**
 * @file views-view-unformatted.tpl.php
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */
?>
<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php $rows = array_unique($rows); ?>
<?php foreach ($rows as $id => $row): ?>
  <div class="<?php print $classes[$id]; ?>">
    <?php print $row; ?>
  </div>
<?php endforeach; ?>
WildKitten’s picture

#32 was good until I had to use pager. Pager was made based on original number of rows so it was wrong for new number of rows. So I did something else.

I had multiple terms from same vocabulary in one node.
In view I had this:
Filds: Content Photo.
Filters: Tahonomy Term (exposed).

So, unchecked "Group multiple values" for field that has multiple values (in my case it was Photo).

In custom module add this function:


function YOURMODULE_views_pre_execute(&$view) {
  if ($view->name == 'YOURVIEW' && $view->current_display == 'DISPLAYOFYOURVIEW') {
    $query = &$view->build_info['query'];
    $change_with_distinct = 'node.nid AS nid';
    $distinct = "DISTINCT node.nid AS nid";
    $query = str_ireplace($change_with_distinct, $distinct, $query);
	
    $view->build_info['count_query']=$view->build_info['query'];
  }
}

That is all. Adding DISTINCT after SELECT in query removes all duplicate rows.
And $view->build_info['count_query']=$view->build_info['query']; rebuild new pager for new query.

Hope this will help someone.

fadgadget’s picture

Thanks WildKitten. This is the same problem ive had for ages now. I just cant fix it. Ive only got 2 terms per node both in the same vocab on my frontpage view and i get duplicates. It's a bugger. Before i go on my voyage of drupal discovery over this is there really no other way to fix this? I thought Views could deal with more than one term at a time to make an easy enough display without duplicates. Could anyone tell me what i might need to change in a view to remove duplicates?

-Same vocab
-sorted by comment time
-no arguments or relationships.
-no tax image

Hopefully by the time i finish the rest of my site folks will know when ive got single entry posts on the latest page then i might be almost done lol

WildKitten’s picture

@fadgadget : #33 works like a charm form me. Have you tried to do that? Also mu CCK field (Photo) has unlimited values.

doomed’s picture

Zoo33 is right.

If you are filtering by taxonomy and also sorting by the same taxonomy, you are bound to have duplicates.

nally’s picture

A solution that I found worked well for me was to form a View not of nodes, but a View of taxonomy terms and then use a relationship to find all nodes with that term applied. Your mileage may vary.

japo32’s picture

thank you!

pzula’s picture

Hi Nally,

Can you describe this process a little? Does creating a view of taxonomy terms mean that your "fields" was set to "Content: all taxonomy terms"?

lykyd’s picture

My problem was that I was using the 'taxonomy_term_data' (Taxonomy Term) instead of using the field that references the terms of the node 'field_data_field_article_tags' (Content) in my case.

Now I can use Distinct to remove duplicate results. I'm using a Node View.

akki123’s picture

Issue summary: View changes

After five years
@dzieyzone : #23 is still the issue, but solution was not given.
A detail solution - http://drupal.stackexchange.com/questions/106422/why-does-views-add-the-sort-field-as-a-select-expression

So for query in #23 , hook_views_query_alter will be

function mymodule_views_query_alter(&$view, &$query) {
 
   if ($view->name == 'my_view') {
	unset($query->fields['term_node_weight_in_tid']);
	$query->orderby[0]['field'] = "term_node.weight_in_tid";
  }
}
millionleaves’s picture

I experienced this problem on my site. Hopefully my solution might help others with the same problem.

  1. View shows products (nodes) in a store.
  2. I added the Category field to the field list so I could group the products by category.
  3. I added a Term relationship to the view, so I could sort the terms by weight.
  4. When I assigned more than one category to a product, the product would appear twice in each assigned category.

My solution was to add an Entity Reference relationship to the Catalogue vocabulary and use that to obtain the weight value for the term (I am using Entity Reference, not Term Reference). This allowed me to keep the grouping AND sort the terms by weight. I did not need to set the view to Distinct.

millionleaves’s picture

Here's another solution inspired by #33 (didn't work in D7) and #41 (not quite the solution I needed), with help from https://www.drupal.org/node/1565480#comment-10418017.

Essentially, it's a custom module that alters the view by adding a Group By nid clause to the query generated by the view.

function YOURMODULE_views_query_alter(&$view,&$query) {
  if ($view->name == 'YOURVIEW' && $view->current_display == 'YOURVIEWDISPLAY') {
	$query->add_field('node', 'nid', 'node_nid', array('function' => 'groupby'));
        $query->add_groupby('node.nid');
  }
}

This solved my problem.

Note that you need to specify the View and (optionally) the Display that you want to alter.

For reference, my problem was that I had a product catalogue where products could be assigned more than one term within the Catalogue vocabulary. These nodes would then appear more than once on each related Catalogue page. This module fixed that.

bloke_zero’s picture

@millionleaves - looks like the right solution - neat. Thanks for sharing!

philsward’s picture

Ran into this with Draggable views. Trying the suggestion about removing the weight sort filter, I removed the draggable views weight filter, re-added it and it appears to have fixed my duplicate problem.

Oddly, the duplicates were random. Some were duplicated, others were not and there was no rhyme or reason to it.

I guess the moral of the story is to create the proper contextual filter first, then add the weight? I spent 2 days off-and-on trying to find the "right" contextual filter only to now realize, it was (technically) a Views "bug" (design?) all along.