There are several reasons to overwrite an SQL query generated by a view whether it’s to improve performance or join to tables not available to the view.
After setting up the view, create a module with a function using hook_views_pre_execute.
function [module name]_views_pre_execute(&$view) {
if($view->name==”[view name]”) {
$sql = “[the new query here]”;
$view->build_info[‘query’] = $sql;
}
}
If you need to access arguments passed to the view look in $view->build_info[‘query_args’].