Home > Enterprise >  Create table with aggregate data from another table, using SQL
Create table with aggregate data from another table, using SQL

Time:01-22

I am trying to create the tableau_table_1 table from the aggregated data of the covid_deaths table.

Here is my query...

CREATE TABLE tableau_table_1 AS 
SELECT 
   SUM(new_cases) AS total_cases,
   SUM(cast(new_deaths as int)) AS total_deaths, 
   SUM(cast(new_deaths as int))/SUM(New_Cases)*100 AS death_percentage 
FROM
   covid_data..covid_deaths 
WHERE continent IS NOT NULL
 -- Group By date order by 1,2

The error that I am showing is...

Incorrect syntax near the keyword 'from'

CodePudding user response:

You could try SELECT INTO Statement

SELECT * INTO tableau_table_1
FROM (
   SELECT 
      SUM(new_cases) AS total_cases,
      SUM(cast(new_deaths AS int)) AS total_deaths, 
      SUM(cast(new_deaths AS int))/SUM(New_Cases)*100 AS death_percentage 
   FROM
      covid_data..covid_deaths 
   WHERE continent IS NOT NULL) AS aggregated_data;
  • Related