It's been a while since I've been so impressed with a module. This allows you to create views based of SQL. You allow it to analyze your SQL and it will populate a list of options for each field your query returns.
You can write module hooks to handle the query output on a per-field basis, or you can write the handler directly in the configuration.
Rather than trying to understand it form this article, take a look at The Very Extensive Video.