求教一个SQL语句的写法
有表a如下:
id money
01 1万
02 4.5万
03 1万
04 2万
05 4万
06 10万
07 10万
08 8万
09 15万
10 14万
要求通过SQL语句显示如下结果:
1-3万 3
3-5万 2
5—10万 3
10万以上 2
请教如何用一条SQL 语句写出来?
问题解决马上给分,可加分
------解决方案--------------------Select N '1-3万 ' As [Money], Count(id) As [Count] From A Where Cast(Replace([money], N '万 ', ' ') As Numeric(10, 1)) Between 1.0 And 3.0
Union All
Select N '3-5万 ' As [Money], Count(id) As [Count] From A Where Cast(Replace([money], N '万 ', ' ') As Numeric(10, 1)) Between 3.0 And 5.0
Union All
Select N '5-10万 ' As [Money], Count(id) As [Count] From A Where Cast(Replace([money], N '万 ', ' ') As Numeric(10, 1)) Between 5.0 And 10.0
Union All
Select N '10万以上 ' As [Money], Count(id) As [Count] From A Where Cast(Replace([money], N '万 ', ' ') As Numeric(10, 1)) > 10.0
------解决方案--------------------select case when cast(replace([money], '万 ', ' ') as numeric(18,2)) <=3.0 then '1-3万 '
when cast(replace([money], '万 ', ' ') as numeric(18,2))> 3.0 and cast(replace([money], '万 ', ' ') as numeric(18,2)) <=5.0 then '3-5万 '
when cast(replace([money], '万 ', ' ') as numeric(18,2))> 5.0 and cast(replace([money], '万 ', ' ') as numeric(18,2)) <=10.0 then '5-10万 '
when cast(replace([money], '万 ', ' ') as numeric(18,2))> 10.0 then '10万以上 '
end as 范围,
count(*) as 个数
from a
group by case when cast(replace([money], '万 ', ' ') as numeric(18,2)) <=3.0 then '1-3万 '
when cast(replace([money], '万 ', ' ') as numeric(18,2))> 3.0 and cast(replace([money], '万 ', ' ') as numeric(18,2)) <=5.0 then '3-5万 '
when cast(replace([money], '万 ', ' ') as numeric(18,2))> 5.0 and cast(replace([money], '万 ', ' ') as numeric(18,2)) <=10.0 then '5-10万 '
when cast(replace([money], '万 ', ' ') as numeric(18,2))> 10.0 then '10万以上 '
end