Is it possible to count the numer of node references and use this value in views?

Let's say we have company-nodes and employee-nodes and every employee has a node reference to 1 company.

I want to get a simple view with a table with companies and the number of emplyees in this company based on the node referenes to emplyees.

View:

company | employess
--------------------
Dell | 20
IBM | 25

Does anyone know how to do this?

(I already show a company-node with a list of emplyees of this company,based on argument & views & view references. So it shoulded be difficult to count the number of nodes in this view)

Comments

Walterh’s picture

problem solved by using a computed field with the code:

Computed Code:

$node_field[0]['value'] = db_result(db_query("SELECT COUNT(*) FROM content_type_x where `field_product_nid` = $node->nid "));

Display Format:

$display = db_result(db_query("SELECT COUNT(*) FROM content_type_x where `field_product_nid` = $node->nid "));
nogat’s picture

sorry if i ask after some time is passed.

could someone please explain where to put these code snippets for computing and displaying node count? i have same need and i am trying to do same thing.

thank you

UPDATE: sorry i didn't realize it was a computed field...

caschbre’s picture

I'm looking to the same thing.

Is using a computed field the only way to accomplish this?

Is creating a view with these SQL statements in them going to be a burden on the server performance-wise?

pingo’s picture

Is there no other way of doing this, than using the computed fields?

Stomper’s picture

I am also looking for a way to display the number of node references in a View similar to the "comment count" field available in Views.

Can this module be used, Node Reference Count (http://drupal.org/project/nodereference_count)?

It seems the node reference count calculated by the "computed field" module cannot be used with views since it is only computed when the node is loaded. See "The field will not work in views nor will it function properly if it depends on other non-stored computed fields." (here: http://drupal.org/node/126522).

Stomper’s picture

Just tried it with the nodereference_count module. It worked fine. I was able to display the node reference count using a custom view though I needed to run VBO.

For users with preexisting content with node references you will need the VBO (views bulk operations) module and have to run it in order for the nodereference count to be calculated and applied to them.

Read the "README" that comes with the module.

mrchristoph’s picture

I'm trying to do the same thing in Drupal 7, but I just cant get any code to work! The module above has not yet been developed for Drupal 7 :(

creatile’s picture

I am also looking for a solution in Drupal 7

- Guillaume -

Stomper’s picture

Well there is a D7 dev available for the nodereference count module. Perhaps test it out and provide feedback to the developer.

Also, it may make more sense to talk about D7 issues on the project page http://drupal.org/project/nodereference_count