Insert Group By count results into a table
How do you insert a group by count result into a table? I'm trying to insert a list of开发者_JAVA百科 names with counts for each.
Thanks!!
You make a select query that gives you the result that you want, then you just put the insert in front of it. Example:
insert into NameCount (Name, Cnt)
select Name, count(*)
from Persons
group by Name
It probably depends on the exact RDBMS you are using, but this syntax is common for the task:
insert into groupTable(name, count)
select name, count(*) as count from people
group by name
This supposes you already have created the groupTable table. Some engines allow you to create the table directly from the query
create table groupTable as
select name, count(*) as count from people
group by name
精彩评论