啊,关于sql的sum,奇怪的加法。

来源:百度知道 编辑:UC知道 时间:2024/09/25 22:22:59
啊,关于sql的sum,奇怪的加法。具体:一个表中有“国家”列,“人数”列,国家列对应的数据为“美国,法国,英国,韩国,美国 英国,美国 法国”6行数据,对应的人数分别为“3,54,2,4,6,67”,现在我要分别计算出每个国家对应的总人数,提示,比如说“美国 法国”对应67人,计算的时候要是美国67,法国67的意思。表的结构大致为:
国家 人数
美国 3
法国 54
英国 2
韩国 4
美国 英国 6
美国 法国 67

求,每个国家对应的人数和,美国的人数和是3+6+67,英国的人数和为2+6,法国的人数和为54+67.
可以使用存储过程实现,也可以使用sql语句实现。定重谢!
QQ:510621956

select sum(人数) from table where 国家 like '%美国%' union all
select sum(人数) from table where 国家 like '%法国%' union all
select sum(人数) from table where 国家 like '%英国%' union all
select sum(人数) from table where 国家 like '%韩国%'

select
A.[国家],
(select Sum([人数]) from table1 where [国家] like '%'+A.[国家]+'%') as [总人数]
from
(select distinct [国家] from table1 where [国家] not like ' ' ) A

look,经oracle测试可用。
select Temp.国家,
(select Sum(人数) from 表名 where instr(表名.国家,Temp.国家)>0) as 总人数
from
(select distinct 国家 from 表名 where 国家 not like '% %') Temp

设表test12(nation,num)
适用:对于某个国家,必须要有单个国家人数(即是有('nation',num)),
如对数据('美国 伊拉克',100),如果没有伊拉克单个国家的数据就会查不到伊拉克的人数.

select nation,sum(num) from
((select t1.nation nation,t2.num num from
(select * from test12 where length(nation)=6)t1,
(select * from test12 where length(nation)&g