Use select results as a table

You may sometimes want to query further on query results in order to make the querying logic simple. I’ve used views to do that in the past when the underlying query has to be reused over and over again. But for temporary purposes, you could use the SELECT query results as a table and query over it.

e.g.
I wanted to filter results from a table and then group the results further. I filtered the data using a select statement and used it as a table for the outer query for grouping.

SELECT t.server, count(*), report_date
FROM 
(SELECT * FROM kw_server_raw
   WHERE data LIKE '%microsoft_reporting%'
   AND report_date < '8/5/2016'
   ORDER BY id, report_date) t
GROUP BY t.server, report_date;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s