我有一个表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好像不起作用
其实我最初的想法是:
select table1.region,count(distinct table1.country) as country,sum(table1.Num) as Num from table1 group by table1.region
但好像count里是不能包含distinct的