This may no longer be appropriate for you, but there seems to be a very useful discussion on Drupal.org in Implementing a Custom SQL Query for Views / Filters , which seem to answer my similar question.
In particular, the original poster suggested connecting to the hook views_views_pre_execute , which someone else mentioned, can be placed in a custom module, for example:
function mymodulename_views_pre_execute(&$view) { if($view->name=="groups_list2") { // ... $view->build_info['query'] = "SELECT node.nid AS nid ". "FROM node WHERE node.type='%s'"; // wrapped for legibility } }
And another poster noted mySQL Views and the Table Wizard ( direct link ), although they mentioned what to keep in mind in this article about mySQL Views performance
Of course, it's worth reading the entire thread on Drupal.org, although I found it really useful; I hope someone else does too.
Update: Indeed, this is exactly what we are doing now, overriding views_views_pre_execute in a custom module to enter new SQL before it gets into the database. I opened a similar (but more specific) question on Drupal.SE in Slow query with a large data set in Drupal views - is it better to handle it in SQL or PHP? which you may find useful.
source share