如题,如果同一个名字在同一省,出现了很多次,要如何都只算1次呢?
select 省, min(姓名) as 姓名, count(姓名) as 次数, sum(金额) as 金额 from table group by 省
我觉着应该是这样
select 省, count(姓名), sum(total) from (
select 省, 姓名, sum(金额) total from table group by 省,姓名
) a
group by 省
试一下这个: select 省, count(distinct 姓名), sum(金额) from table group by 省
select 省, count(distinct 姓名), sum(金额) from table group by 省,姓名
关键是distinct,用于返回不同的值