You can do this partially with .htaccess files, but some of the configuration will have to be done at the httpd.conf level. Basically, it'd looked like this:
a) Configure your domain to have wildcard dns for the zone. Exact details on how to do this depends on who your DNS provider is, or which BIND software you're running. But basically set things up so that *.example.com points at your server's address.
b) Configure the webserver like this:
<VirtualHost x.x.x.x:80>
ServerName *.example.com
...
</VirtualHost>
<VirtualHost x.x.x.x:80>
Server some.fixed.subdomain.example.com
...
</VirtualHost>
Make sure you list any non-dynamic domains AFTER the wildcard entry, or things will most likely not work. Apache's rather picky about the order this gets set up in.
With this setup, there's no need to rewrite requests into a query. You can have your scripts check $_SERVER['HTTP_HOST']
to figure out which virtual sub-domain is being served and work from there.