Here's my problem hopefully someone can help!

I'm trying to make a view/report which shows the number of nodes posted per DAY. So the first column of each row will be the date (day) like Feb 05 2012, then the next column will be a count of the number of nodes posted on that day (like 37). So this is a view of nodes, with aggregation, doing a "count" on the NID field, and setting the sort criteria to "post date" so the aggregation knows to aggregate on post date.

No matter how I change the granularity of post date, the aggregation stays in "seconds". Meaning I get how many nodes are posted at exactly the same second, but not how many are posted on a given day, I get duplicate rows. etc.

I think this is a bug, because I'm easily able to fix this and get the correct result by modifying the output query and running it manually.

Here's the query that views produces:

SELECT node.created AS node_created, COUNT(node.nid) AS nid, DATE_FORMAT((DATE_ADD('19700101', INTERVAL node.created SECOND) + INTERVAL -18000 SECOND), '%Y%m%d') AS node_created_day
FROM 
{node} node
WHERE (( (node.status = '1') ))
GROUP BY node_created
ORDER BY node_created_day DESC

If it could just be changed to this, it would work. Note the GROUP BY clause using "node_created_day" instead of just "node_created"

SELECT node.created AS node_created, COUNT(node.nid) AS nid, DATE_FORMAT((DATE_ADD('19700101', INTERVAL node.created SECOND) + INTERVAL -18000 SECOND), '%Y%m%d') AS node_created_day
FROM 
{node} node
WHERE (( (node.status = '1') ))
GROUP BY node_created_day
ORDER BY node_created_day DESC
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

The funny thing here is that this is actually invalid sql. Could you be so kind and provide an export to the view, so it's possible to see how it's actually configured, maybe this gives some ideas.

wodenx’s picture

Status: Active » Needs review
FileSize
4.76 KB
956 bytes

I had this problem in 6.x-3.x, but was able to resolve it there by changing the aggregation settings on the date field (not the date sort field) to "min" or "max" - this removed that field from the GROUP BY, and left me with:

SELECT COUNT(node.nid) AS nid, MIN(node.created) AS node_created, DATE_FORMAT((FROM_UNIXTIME(node.created) + INTERVAL -18000 SECOND), '%Y%m%%d') AS node_created_day FROM node node  GROUP BY node_created_day ORDER BY node_created_day ASC

which worked (except for the fact that the formatting for the aggregated date field was lost -- but that's another issue).

But this doesn't seem to work in D7 - the GROUP BY for the sort field is never added to the query. The attached patch seems to fix this. I've also attached an export of the faulty view.

I also think it would be good to provide granularity settings for the default date field handler -- that way it could be used for grouping without having to add the ORDER BY as well (in case you want to sort by something else). I would be willing to submit a patch for this if it is thought desirable.

tim.plunkett’s picture

Triggering the testbot.

dawehner’s picture

Issue tags: +Needs tests

Add a tag

tim.plunkett’s picture

Version: 7.x-3.1 » 7.x-3.x-dev

Changing version for the testbot.

dawehner’s picture

Status: Needs review » Fixed

Just played a bit with the problem and your patch is totally right, thank you!

Committed the patch to 7.x-3.x

Status: Fixed » Closed (fixed)

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

mototribe’s picture

I didn't try the patch but your trick to change it to min or max worked great. Thanks!

lunk rat’s picture

Woo-hoo! Thanks for the tip in #2. I actually had to change my sort criteria aggregation settings (not the field aggregation) to 'Minimum' and then the granularity worked for my aggregated date field!

What an awesome trick!

Rohit_Rajput’s picture

Status: Closed (fixed) » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: 1431780-aggregate-formulae.patch, failed testing.

Leeteq’s picture

Ref. also this module that handles the same, AFAICS;

https://www.drupal.org/project/views_date_format_sql