How do you count the number of rows in a table that have the same value in value in a column?

耗尽温柔 提交于 2021-01-29 10:20:15

问题


Suppose I have the following simple table:

create table mytable (
    desid bigint not null,
    ancid bigint not null
  );

insert into mytable (ancid,desid) values (1,10);
insert into mytable (ancid,desid) values (1,20);
insert into mytable (ancid,desid) values (1,21);
insert into mytable (ancid,desid) values (1,22);
insert into mytable (ancid,desid) values (2,30);
insert into mytable (ancid,desid) values (3,40);
insert into mytable (ancid,desid) values (3,41);
insert into mytable (ancid,desid) values (3,42);
insert into mytable (ancid,desid) values (3,43);

What is the SQL command that will print out the following information:

4 rows with ancid=1
1 rows with ancid=2
4 rows with ancid=3

回答1:


You can get the information by grouping by the ancid:

SELECT   ancid, COUNT(*)
FROM     mytable
GROUP BY ancid


来源:https://stackoverflow.com/questions/65782256/how-do-you-count-the-number-of-rows-in-a-table-that-have-the-same-value-in-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!