views:

59

answers:

2

How do you insert a group by count result into a table? I'm trying to insert a list of names with counts for each.

Thanks!!

A: 

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
Guffa
A: 

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
Vinko Vrsalovic