开发者

Insert Group By count results into a table

开发者 https://www.devze.com 2022-12-13 04:04 出处:网络
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.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消