I'm wanting to be able to build a view using a query as the base 'table', is this possible with Views 2.0 and how might I do it?

Comments

dawehner’s picture

You want to have a query and manipulate the query with views? This is afaik not supportet, views is a query builder not a query alter :)

greenskin’s picture

I want to build a query on top of another query. Whether the base query is from Views or hardcoded, either way. Essentially I want to build a View from a 'table' that is actually a query. For example the Views query would look something like this: 'SELECT * FROM ({BASE_QUERY}) AS base' instead of 'SELECT * FROM base_table AS base'. {BASE_QUERY} would be the query string. Obviously my example is not exactly how Views constructs aliases or selections.

merlinofchaos’s picture

I don't know if you can do this. My guess is that you can't.

esmerel’s picture

Status: Active » Closed (works as designed)

Merlin doens't think it can be done, it probably can't.

greenskin’s picture

Version: 6.x-2.x-dev » 6.x-3.x-dev
Category: support » feature
Status: Closed (works as designed) » Active

I have done it by simply specifying my SQL code as the base table name, but was wondering if there is a way that would be more elegant. I guess this is turning out to be more of a feature request than a support request. I know that Views SQL's can get rather massive, but I think it would be a great feature if a view could be built off of another view. Essentially another view would be the 'base table'.

All Views should have to do is recognize the 'base table' is a view rather than a database table, build that view's SQL and insert that as the FROM table. For example, SELECT * FROM ({VIEW_SQL}) AS view_base. The parent view would inherit the table relationships for the base view. If the base view used the base table of {node} then tables being added to the parent view would 'JOIN {node_revision} AS node_revision ON view_base.nid = node_revision.nid'.

I don't use PostgreSQL so I don't know if it is possible to do subqueries in it or not. That might be the biggest limitation in implementing this type of functionality.

I'll make this a request for the 3.x branch.

I have a project that I would like to have the control over my SQL code via Views, but they require subqueries otherwise I would have to make multiple calls to the database and load data I don't actually need.

esmerel’s picture

Status: Active » Postponed
bojanz’s picture

Status: Postponed » Closed (won't fix)

That's a hack.
Make a contrib module that does it, and does it well, then we can return to the idea (and maybe merge it into Views).