SQL statement to create a table as a result of a count operation?

Say you have a table like this

id   terms    
1    a       
2    c       
3    a       
4    b       
5    b       
6    a       
7    a
8    b
9    b
10   b        

and you want to receive such a report;

terms  count
a      4
b      5
c      1

So you run this on the first table

SELECT terms, COUNT( id) AS count 
    FROM table 
GROUP BY terms 
ORDER BY terms DESC

So far so good.

But the above SQL status puts the report view in the browser. Well, I want to save this data in SQL.

So, what SQL command do I need to insert the results of this report into the table?

Suppose you have already created a table with a name reportswith this:

create table reports (terms varchar(500), count (int))

Suppose the table is reportsempty, and we just want to fill it with the following view - using a single line. The question I am asking is how?

  terms  count
    a      4
    b      5
    c      1
+1
source share
2 answers

The easiest way:

INSERT INTO reports
SELECT terms, COUNT( id) AS count 
FROM table 
GROUP BY terms 
ORDER BY terms DESC
+5
source

:

Insert reports
SELECT terms, COUNT(*) AS count 
FROM table 
GROUP BY terms 

:

SELECT terms, COUNT(*) AS count 
into reports
FROM table 
GROUP BY terms
+1

All Articles