Skip to content
Advertisement

Is there a way to turn the results of a SELECT, FROM, GROUPBY statement into a new table?

Basically I need to put some tables from SQL into Excel to analyze it with a third-party program. However, when using Excel, the PivotTable date columns are in a specific format (d mm) and for the third-party program it will not accept it.

However, in SQL where I have done some aggregations on the raw data, the format that I have used for the aggregation statement is absolutely correct and I would like to turn those results into a separate table so that I can feed it to the external program. I will have an example of what I mean below.

This is the resulting view of what the SQL aggregation statement on the data looks like.

For reference, this is the aggregation statement that I have made.

Basically I would like to turn that view above the code statement into a table with exactly those values and headers into a separate table that can be interacted with.

Advertisement

Answer

SELECT INTO is what you want to use here. Assuming you have the permissions to create a new table the steps would be:

  1. Create the table you want to insert the data in
  2. Convert the SELECT query into a SELECT INTO query.

Here’s an example from W3 Schools I’ve modified for your use case that should work. The main thing is that the new_table should already be created then modify your select query by adding an INTO:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement