日期:2014-05-18  浏览次数:20655 次

怎样按字母和数字区别统计?
原表
客户编码 销售额(元)
CUSA00001 10 ----------美国客户
CJPN00001 10 ----------日本客户
C05140001 5 ----------扬州客户
C02100001 5 ----------上海客户
CGER00001 8 ----------德国客户
C05510001 5 ----------合肥客户

都是客户,首字母都是C 第二位开始区分外国客户和国内客户
国内的用区号,也就是数字表示
国外的用国际三字码,也就是的认为开始有几位字母

怎样通过这个区别统计国际和国外的总销售额

结果输出

国内 15元
国外 28元


还请赐教



------解决方案--------------------
SQL code
select (case when substring(客户编码,2,1)>'9' then '国外' else '国内' end) as 客户,sum(销售额)
from tb group by (case when substring(客户编码,2,1)>'9' then '国外' else '国内' end)

------解决方案--------------------
SQL code
select
 (case when substring(客户编码,2,1)>'9' then '国外' else '国内' end) as 客户,
 sum(销售额)
from
 tb 
group by
 (case when substring(客户编码,2,1)>'9' then '国外' else '国内' end)

------解决方案--------------------
SQL code
select 客户=(case when substring(客户编码,2,1)>'9' then '国外' else '国内' end),
      sum(销售额)
from tb group by (case when substring(客户编码,2,1)>'9' then '国外' else '国内' end)

------解决方案--------------------
SQL code
select case when substring(客户编码,2,1) between '0' and '9' then '国内' else '国外' end , sum(销售额) 销售额
from 原表
group by case when substring(客户编码,2,1) between '0' and '9' then '国内' else '国外' end