标题: access怎么统计某一列不重复记录的个数 [打印本页] 作者: cathyaccess 时间: 2009-7-24 01:16 标题: access怎么统计某一列不重复记录的个数 我有一个表table1:
region country Num
A a 3
A b 1
B c 1
B c 2
C d 4
SQL语句怎么写,才能产生一个下面的新表table2:
region country Num
A 2 4
B 1 3
C 1 4
也就是说,统计A区域里有几个国家的记录,并且这几个国家的Num总和是多少。
select table1.region,(count(*) from (select distinct(table1.region) from region group by table1.region)) as country,sum(table1.Num) as Num from table1 group by table1.region
这样做下来,country的数总是不对,最后的group by好像不起作用作者: cathyaccess 时间: 2009-7-24 01:22
其实我最初的想法是:
select table1.region,count(distinct table1.country) as country,sum(table1.Num) as Num from table1 group by table1.region
但好像count里是不能包含distinct的作者: cathyaccess 时间: 2009-7-24 01:38
谢谢大家了,我已经回了作者: cathyaccess 时间: 2009-7-24 01:41
直接用
select table1.region,count(table1.country) as country,sum(table1.Num) as Num from table1 group by table1.region
就实现了,由于table1有错,我还以为语句有问题:)