Skip to content

Instantly share code, notes, and snippets.

@greggles
Last active January 2, 2016 12:19
Show Gist options
  • Save greggles/8302571 to your computer and use it in GitHub Desktop.
Save greggles/8302571 to your computer and use it in GitHub Desktop.
<?php
// Query code from http://drupalcode.org/project/project.git/blob/refs/heads/7.x-2.x:/usage/includes/pages.inc#l76
$query = db_select('node', 'n')
->fields('n', array('nid', 'title'))
->condition('n.nid', $usage_week_nids, 'IN')
->condition('n.status', NODE_PUBLISHED)
->groupBy('n.nid')
->groupBy('n.title')
->addTag('node_access');
$query->addExpression("SUM(DISTINCT p{$i}.count)", "week{$i}");
$query->leftJoin('project_usage_week_project', "p{$i}", "n.nid = p{$i}.nid AND p{$i}.timestamp = :timestamp{$i}", array(":timestamp{$i}" => $week));
$query
->extend('TableSort')
->orderByHeader($headers)
->extend('PagerDefault')
->limit(100)
->execute();
/**
SELECT n.nid, n.title, SUM(DISTINCT p1.count) as week1
FROM node n
LEFT JOIN project_usage_week_project p1 ON n.nid = p1.nid p1.timestamp = (SELECT MAX(timestamp) FROM project_usage_week_project)
WHERE n.status = 1
GROUP BY n.nid, n.title
limit 100
SORT BY week1 DESC
LIMIT 1
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment